Skip to content

Commit

Permalink
Merge pull request iluwatar#10 from mafagafogigante/mafagafo-observer
Browse files Browse the repository at this point in the history
Changes to the observer pattern
  • Loading branch information
iluwatar committed Oct 21, 2014
2 parents 6faa8df + 5109c8f commit 9b1c698
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 22 deletions.
Binary file modified observer/etc/observer.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 2 additions & 4 deletions observer/src/main/java/com/iluwatar/Hobbits.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ 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.");
Expand All @@ -16,8 +15,7 @@ public void update(WeatherType currentWeather) {
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;
Expand Down
20 changes: 3 additions & 17 deletions observer/src/main/java/com/iluwatar/Weather.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,9 @@ public void removeObserver(WeatherObserver obs) {
}

public void timePasses() {
switch (currentWeather) {
case COLD:
currentWeather = WeatherType.SUNNY;
break;
case RAINY:
currentWeather = WeatherType.WINDY;
break;
case SUNNY:
currentWeather = WeatherType.RAINY;
break;
case WINDY:
currentWeather = WeatherType.COLD;
break;
default:
break;
}
System.out.println("The weather now changes to " + currentWeather);
WeatherType[] enumValues = WeatherType.values();
currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
System.out.println("The weather changed to " + currentWeather + ".");
notifyObservers();
}

Expand Down
3 changes: 2 additions & 1 deletion observer/src/main/java/com/iluwatar/WeatherType.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ public enum WeatherType {

SUNNY, RAINY, WINDY, COLD;

@Override
public String toString() {
return this.name().toLowerCase();
};
}

}

0 comments on commit 9b1c698

Please sign in to comment.