Formatted all files to the same standard
This commit is contained in:
@ -1,23 +1,23 @@
|
||||
package com.iluwatar;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* Observer pattern defines one-to-many relationship between objects. The target
|
||||
* object sends change notifications to its registered observers.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) {
|
||||
|
||||
Weather weather = new Weather();
|
||||
weather.addObserver(new Orcs());
|
||||
weather.addObserver(new Hobbits());
|
||||
Weather weather = new Weather();
|
||||
weather.addObserver(new Orcs());
|
||||
weather.addObserver(new Hobbits());
|
||||
|
||||
weather.timePasses();
|
||||
weather.timePasses();
|
||||
weather.timePasses();
|
||||
weather.timePasses();
|
||||
weather.timePasses();
|
||||
weather.timePasses();
|
||||
weather.timePasses();
|
||||
weather.timePasses();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,8 @@ public class Hobbits implements WeatherObserver {
|
||||
public void update(WeatherType currentWeather) {
|
||||
switch (currentWeather) {
|
||||
case COLD:
|
||||
System.out.println("The hobbits are shivering in the cold weather.");
|
||||
System.out
|
||||
.println("The hobbits are shivering in the cold weather.");
|
||||
break;
|
||||
case RAINY:
|
||||
System.out.println("The hobbits look for cover from the rain.");
|
||||
@ -15,7 +16,8 @@ public class Hobbits implements WeatherObserver {
|
||||
System.out.println("The happy hobbits bade in the warm sun.");
|
||||
break;
|
||||
case WINDY:
|
||||
System.out.println("The hobbits hold their hats tightly in the windy weather.");
|
||||
System.out
|
||||
.println("The hobbits hold their hats tightly in the windy weather.");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
@ -5,28 +5,28 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* Weather can be observed by implementing WeatherObserver
|
||||
* interface and registering as listener.
|
||||
*
|
||||
* Weather can be observed by implementing WeatherObserver interface and
|
||||
* registering as listener.
|
||||
*
|
||||
*/
|
||||
public class Weather {
|
||||
|
||||
private WeatherType currentWeather;
|
||||
private List<WeatherObserver> observers;
|
||||
|
||||
|
||||
public Weather() {
|
||||
observers = new ArrayList<>();
|
||||
currentWeather = WeatherType.SUNNY;
|
||||
}
|
||||
|
||||
|
||||
public void addObserver(WeatherObserver obs) {
|
||||
observers.add(obs);
|
||||
}
|
||||
|
||||
|
||||
public void removeObserver(WeatherObserver obs) {
|
||||
observers.remove(obs);
|
||||
}
|
||||
|
||||
|
||||
public void timePasses() {
|
||||
switch (currentWeather) {
|
||||
case COLD:
|
||||
@ -47,9 +47,9 @@ public class Weather {
|
||||
System.out.println("The weather now changes to " + currentWeather);
|
||||
notifyObservers();
|
||||
}
|
||||
|
||||
|
||||
private void notifyObservers() {
|
||||
for (WeatherObserver obs: observers) {
|
||||
for (WeatherObserver obs : observers) {
|
||||
obs.update(currentWeather);
|
||||
}
|
||||
}
|
||||
|
@ -3,10 +3,10 @@ package com.iluwatar;
|
||||
/**
|
||||
*
|
||||
* Observer interface.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public interface WeatherObserver {
|
||||
|
||||
void update(WeatherType currentWeather);
|
||||
|
||||
|
||||
}
|
||||
|
@ -2,13 +2,10 @@ package com.iluwatar;
|
||||
|
||||
public enum WeatherType {
|
||||
|
||||
SUNNY,
|
||||
RAINY,
|
||||
WINDY,
|
||||
COLD;
|
||||
|
||||
SUNNY, RAINY, WINDY, COLD;
|
||||
|
||||
public String toString() {
|
||||
return this.name().toLowerCase();
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user