X Tutup
Skip to content

Commit b3d48cc

Browse files
committed
added observer sample
1 parent 6246ed6 commit b3d48cc

File tree

8 files changed

+163
-0
lines changed

8 files changed

+163
-0
lines changed

observer/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<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"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.iluwatar</groupId>
7+
<artifactId>java-design-patterns</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<groupId>com.iluwatar</groupId>
11+
<artifactId>observer</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
<name>observer</name>
14+
<url>http://maven.apache.org</url>
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>3.8.1</version>
20+
<scope>test</scope>
21+
</dependency>
22+
</dependencies>
23+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.iluwatar;
2+
3+
public class App
4+
{
5+
public static void main( String[] args )
6+
{
7+
8+
Weather weather = new Weather();
9+
weather.addObserver(new Orcs());
10+
weather.addObserver(new Hobbits());
11+
12+
weather.timePasses();
13+
weather.timePasses();
14+
weather.timePasses();
15+
weather.timePasses();
16+
17+
}
18+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.iluwatar;
2+
3+
public class Hobbits implements WeatherObserver {
4+
5+
@Override
6+
public void update(WeatherType currentWeather) {
7+
switch (currentWeather) {
8+
case COLD:
9+
System.out.println("The hobbits are shivering in the cold weather.");
10+
break;
11+
case RAINY:
12+
System.out.println("The hobbits look for cover from the rain.");
13+
break;
14+
case SUNNY:
15+
System.out.println("The happy hobbits bade in the warm sun.");
16+
break;
17+
case WINDY:
18+
System.out.println("The hobbits hold their hats tightly in the windy weather.");
19+
break;
20+
default:
21+
break;
22+
}
23+
}
24+
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.iluwatar;
2+
3+
public class Orcs implements WeatherObserver {
4+
5+
@Override
6+
public void update(WeatherType currentWeather) {
7+
switch (currentWeather) {
8+
case COLD:
9+
System.out.println("The orcs are freezing cold.");
10+
break;
11+
case RAINY:
12+
System.out.println("The orcs are dripping wet.");
13+
break;
14+
case SUNNY:
15+
System.out.println("The sun hurts the orcs' eyes.");
16+
break;
17+
case WINDY:
18+
System.out.println("The orc smell almost vanishes in the wind.");
19+
break;
20+
default:
21+
break;
22+
}
23+
}
24+
25+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.iluwatar;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Weather {
7+
8+
private WeatherType currentWeather;
9+
private List<WeatherObserver> observers;
10+
11+
public Weather() {
12+
observers = new ArrayList<>();
13+
currentWeather = WeatherType.SUNNY;
14+
}
15+
16+
public void addObserver(WeatherObserver obs) {
17+
observers.add(obs);
18+
}
19+
20+
public void removeObserver(WeatherObserver obs) {
21+
observers.remove(obs);
22+
}
23+
24+
public void timePasses() {
25+
switch (currentWeather) {
26+
case COLD:
27+
currentWeather = WeatherType.SUNNY;
28+
break;
29+
case RAINY:
30+
currentWeather = WeatherType.WINDY;
31+
break;
32+
case SUNNY:
33+
currentWeather = WeatherType.RAINY;
34+
break;
35+
case WINDY:
36+
currentWeather = WeatherType.COLD;
37+
break;
38+
default:
39+
break;
40+
}
41+
System.out.println("The weather now changes to " + currentWeather);
42+
notifyObservers();
43+
}
44+
45+
private void notifyObservers() {
46+
for (WeatherObserver obs: observers) {
47+
obs.update(currentWeather);
48+
}
49+
}
50+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.iluwatar;
2+
3+
public interface WeatherObserver {
4+
5+
void update(WeatherType currentWeather);
6+
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.iluwatar;
2+
3+
public enum WeatherType {
4+
5+
SUNNY,
6+
RAINY,
7+
WINDY,
8+
COLD;
9+
10+
public String toString() {
11+
return this.name().toLowerCase();
12+
};
13+
14+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<module>iterator</module>
3737
<module>mediator</module>
3838
<module>memento</module>
39+
<module>observer</module>
3940
</modules>
4041

4142
<build>

0 commit comments

Comments
 (0)
X Tutup