added observer sample

This commit is contained in:
Ilkka Seppala 2014-08-22 21:57:54 +03:00
parent 6246ed6d26
commit b3d48cc4df
8 changed files with 163 additions and 0 deletions

23
observer/pom.xml Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.iluwatar</groupId>
<artifactId>observer</artifactId>
<version>1.0-SNAPSHOT</version>
<name>observer</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,18 @@
package com.iluwatar;
public class App
{
public static void main( String[] args )
{
Weather weather = new Weather();
weather.addObserver(new Orcs());
weather.addObserver(new Hobbits());
weather.timePasses();
weather.timePasses();
weather.timePasses();
weather.timePasses();
}
}

View File

@ -0,0 +1,25 @@
package com.iluwatar;
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;
}
}
}

View File

@ -0,0 +1,25 @@
package com.iluwatar;
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;
}
}
}

View File

@ -0,0 +1,50 @@
package com.iluwatar;
import java.util.ArrayList;
import java.util.List;
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:
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);
notifyObservers();
}
private void notifyObservers() {
for (WeatherObserver obs: observers) {
obs.update(currentWeather);
}
}
}

View File

@ -0,0 +1,7 @@
package com.iluwatar;
public interface WeatherObserver {
void update(WeatherType currentWeather);
}

View File

@ -0,0 +1,14 @@
package com.iluwatar;
public enum WeatherType {
SUNNY,
RAINY,
WINDY,
COLD;
public String toString() {
return this.name().toLowerCase();
};
}

View File

@ -36,6 +36,7 @@
<module>iterator</module>
<module>mediator</module>
<module>memento</module>
<module>observer</module>
</modules>
<build>