Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 31 additions & 32 deletions observer/src/main/java/com/iluwatar/observer/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,44 @@

/**
*
* The Observer pattern is a software design pattern in which an object, called
* the subject, maintains a list of its dependents, called observers, and notifies
* them automatically of any state changes, usually by calling one of their methods.
* It is mainly used to implement distributed event handling systems. The Observer
* pattern is also a key part in the familiar model–view–controller (MVC) architectural
* pattern. The Observer pattern is implemented in numerous programming libraries and
* systems, including almost all GUI toolkits.
* The Observer pattern is a software design pattern in which an object, called the subject,
* maintains a list of its dependents, called observers, and notifies them automatically of any
* state changes, usually by calling one of their methods. It is mainly used to implement
* distributed event handling systems. The Observer pattern is also a key part in the familiar
* model–view–controller (MVC) architectural pattern. The Observer pattern is implemented in
* numerous programming libraries and systems, including almost all GUI toolkits.
* <p>
* In this example {@link Weather} has a state that can be observed. The {@link Orcs}
* and {@link Hobbits} register as observers and receive notifications when the
* {@link Weather} changes.
* In this example {@link Weather} has a state that can be observed. The {@link Orcs} and
* {@link Hobbits} register as observers and receive notifications when the {@link Weather} changes.
*
*/
public class App {

/**
* Program entry point
* @param args command line args
*/
public static void main(String[] args) {
/**
* Program entry point
*
* @param args command line 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();

// Generic observer inspired by Java Generics and Collection by Naftalin & Wadler
System.out.println("\n--Running generic version--");
GWeather gWeather = new GWeather();
gWeather.addObserver(new GOrcs());
gWeather.addObserver(new GHobbits());
// Generic observer inspired by Java Generics and Collection by Naftalin & Wadler
System.out.println("\n--Running generic version--");
GWeather gWeather = new GWeather();
gWeather.addObserver(new GOrcs());
gWeather.addObserver(new GHobbits());

gWeather.timePasses();
gWeather.timePasses();
gWeather.timePasses();
gWeather.timePasses();
}
gWeather.timePasses();
gWeather.timePasses();
gWeather.timePasses();
gWeather.timePasses();
}
}
39 changes: 19 additions & 20 deletions observer/src/main/java/com/iluwatar/observer/Hobbits.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@
*/
public class Hobbits implements WeatherObserver {

@Override
public void update(WeatherType currentWeather) {
switch (currentWeather) {
case COLD:
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.");
break;
case SUNNY:
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.");
break;
default:
break;
}
}

@Override
public void update(WeatherType currentWeather) {
switch (currentWeather) {
case COLD:
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.");
break;
case SUNNY:
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.");
break;
default:
break;
}
}
}
39 changes: 19 additions & 20 deletions observer/src/main/java/com/iluwatar/observer/Orcs.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@
*/
public class Orcs implements WeatherObserver {

@Override
public void update(WeatherType currentWeather) {
switch (currentWeather) {
case COLD:
System.out.println("The orcs are freezing cold.");
break;
case RAINY:
System.out.println("The orcs are dripping wet.");
break;
case SUNNY:
System.out.println("The sun hurts the orcs' eyes.");
break;
case WINDY:
System.out.println("The orc smell almost vanishes in the wind.");
break;
default:
break;
}
}

@Override
public void update(WeatherType currentWeather) {
switch (currentWeather) {
case COLD:
System.out.println("The orcs are freezing cold.");
break;
case RAINY:
System.out.println("The orcs are dripping wet.");
break;
case SUNNY:
System.out.println("The sun hurts the orcs' eyes.");
break;
case WINDY:
System.out.println("The orc smell almost vanishes in the wind.");
break;
default:
break;
}
}
}
60 changes: 30 additions & 30 deletions observer/src/main/java/com/iluwatar/observer/Weather.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,38 @@

/**
*
* Weather can be observed by implementing {@link WeatherObserver} interface and
* registering as listener.
* Weather can be observed by implementing {@link 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() {
WeatherType[] enumValues = WeatherType.values();
currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
System.out.println("The weather changed to " + currentWeather + ".");
notifyObservers();
}

private void notifyObservers() {
for (WeatherObserver obs : observers) {
obs.update(currentWeather);
}
}
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() {
WeatherType[] enumValues = WeatherType.values();
currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
System.out.println("The weather changed to " + currentWeather + ".");
notifyObservers();
}

private void notifyObservers() {
for (WeatherObserver obs : observers) {
obs.update(currentWeather);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
*/
public interface WeatherObserver {

void update(WeatherType currentWeather);
void update(WeatherType currentWeather);

}
11 changes: 5 additions & 6 deletions observer/src/main/java/com/iluwatar/observer/WeatherType.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
*/
public enum WeatherType {

SUNNY, RAINY, WINDY, COLD;

@Override
public String toString() {
return this.name().toLowerCase();
}
SUNNY, RAINY, WINDY, COLD;

@Override
public String toString() {
return this.name().toLowerCase();
}
}
36 changes: 18 additions & 18 deletions observer/src/main/java/com/iluwatar/observer/generic/GHobbits.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@
*
*/
public class GHobbits implements Race {
@Override
public void update(GWeather weather, WeatherType weatherType) {
switch (weatherType) {
case COLD:
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.");
break;
case SUNNY:
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.");
break;
default:
break;
}
@Override
public void update(GWeather weather, WeatherType weatherType) {
switch (weatherType) {
case COLD:
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.");
break;
case SUNNY:
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.");
break;
default:
break;
}
}
}
38 changes: 19 additions & 19 deletions observer/src/main/java/com/iluwatar/observer/generic/GOrcs.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@
*
*/
public class GOrcs implements Race {

@Override
public void update(GWeather weather, WeatherType weatherType) {
switch (weatherType) {
case COLD:
System.out.println("The orcs are freezing cold.");
break;
case RAINY:
System.out.println("The orcs are dripping wet.");
break;
case SUNNY:
System.out.println("The sun hurts the orcs' eyes.");
break;
case WINDY:
System.out.println("The orc smell almost vanishes in the wind.");
break;
default:
break;
}

@Override
public void update(GWeather weather, WeatherType weatherType) {
switch (weatherType) {
case COLD:
System.out.println("The orcs are freezing cold.");
break;
case RAINY:
System.out.println("The orcs are dripping wet.");
break;
case SUNNY:
System.out.println("The sun hurts the orcs' eyes.");
break;
case WINDY:
System.out.println("The orc smell almost vanishes in the wind.");
break;
default:
break;
}
}
}
20 changes: 10 additions & 10 deletions observer/src/main/java/com/iluwatar/observer/generic/GWeather.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
*/
public class GWeather extends Observable<GWeather, Race, WeatherType> {

private WeatherType currentWeather;
private WeatherType currentWeather;

public GWeather() {
currentWeather = WeatherType.SUNNY;
}
public GWeather() {
currentWeather = WeatherType.SUNNY;
}

public void timePasses() {
WeatherType[] enumValues = WeatherType.values();
currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
System.out.println("The weather changed to " + currentWeather + ".");
notifyObservers(currentWeather);
}
public void timePasses() {
WeatherType[] enumValues = WeatherType.values();
currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
System.out.println("The weather changed to " + currentWeather + ".");
notifyObservers(currentWeather);
}
}
Loading