Skip to content

Commit 0ead283

Browse files
authored
Merge pull request iluwatar#1475 from ravening/simplify_observer
Use enums instead os switch blocks
2 parents 888af23 + 0c83ccc commit 0ead283

File tree

12 files changed

+56
-130
lines changed

12 files changed

+56
-130
lines changed

observer/README.md

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ tags:
1313
Dependents, Publish-Subscribe
1414

1515
## Intent
16-
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified
16+
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified
1717
and updated automatically.
1818

1919
## Explanation
2020

2121
Real world example
2222

23-
> In a land far away lives the races of hobbits and orcs. Both of them are mostly outdoors so they closely follow the changes in weather. One could say that they are constantly observing the weather.
23+
> In a land far away lives the races of hobbits and orcs. Both of them are mostly outdoors so they closely follow the changes in weather. One could say that they are constantly observing the weather.
2424
2525
In plain words
2626

27-
> Register as an observer to receive state changes in the object.
27+
> Register as an observer to receive state changes in the object.
2828
2929
Wikipedia says
3030

@@ -46,22 +46,7 @@ public class Orcs implements WeatherObserver {
4646

4747
@Override
4848
public void update(WeatherType currentWeather) {
49-
switch (currentWeather) {
50-
case COLD:
51-
LOGGER.info("The orcs are freezing cold.");
52-
break;
53-
case RAINY:
54-
LOGGER.info("The orcs are dripping wet.");
55-
break;
56-
case SUNNY:
57-
LOGGER.info("The sun hurts the orcs' eyes.");
58-
break;
59-
case WINDY:
60-
LOGGER.info("The orc smell almost vanishes in the wind.");
61-
break;
62-
default:
63-
break;
64-
}
49+
LOGGER.info("The hobbits are facing " + currentWeather.getDescription() + " weather now");
6550
}
6651
}
6752

@@ -72,21 +57,7 @@ public class Hobbits implements WeatherObserver {
7257
@Override
7358
public void update(WeatherType currentWeather) {
7459
switch (currentWeather) {
75-
case COLD:
76-
LOGGER.info("The hobbits are shivering in the cold weather.");
77-
break;
78-
case RAINY:
79-
LOGGER.info("The hobbits look for cover from the rain.");
80-
break;
81-
case SUNNY:
82-
LOGGER.info("The happy hobbits bade in the warm sun.");
83-
break;
84-
case WINDY:
85-
LOGGER.info("The hobbits hold their hats tightly in the windy weather.");
86-
break;
87-
default:
88-
break;
89-
}
60+
LOGGER.info("The hobbits are facing " + currentWeather.getDescription() + " weather now");
9061
}
9162
}
9263
```
@@ -141,20 +112,20 @@ Here's the full example in action.
141112

142113
weather.timePasses();
143114
// The weather changed to rainy.
144-
// The orcs are dripping wet.
145-
// The hobbits look for cover from the rain.
115+
// The orcs are facing rainy weather now
116+
// The hobbits are facing rainy weather now
146117
weather.timePasses();
147118
// The weather changed to windy.
148-
// The orc smell almost vanishes in the wind.
149-
// The hobbits hold their hats tightly in the windy weather.
119+
// The orcs are facing windy weather now
120+
// The hobbits are facing windy weather now
150121
weather.timePasses();
151122
// The weather changed to cold.
152-
// The orcs are freezing cold.
153-
// The hobbits are shivering in the cold weather.
123+
// The orcs are facing cold weather now
124+
// The hobbits are facing cold weather now
154125
weather.timePasses();
155126
// The weather changed to sunny.
156-
// The sun hurts the orcs' eyes.
157-
// The happy hobbits bade in the warm sun.
127+
// The orcs are facing sunny weather now
128+
// The hobbits are facing sunny weather now
158129
```
159130

160131
## Class diagram

observer/etc/observer.urm.puml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ package com.iluwatar.observer {
3333
+ RAINY {static}
3434
+ SUNNY {static}
3535
+ WINDY {static}
36+
+ description String
3637
+ toString() : String
38+
+ getDescription() : String
3739
+ valueOf(name : String) : WeatherType {static}
3840
+ values() : WeatherType[] {static}
3941
}
@@ -71,10 +73,10 @@ package com.iluwatar.observer.generic {
7173
Weather --> "-currentWeather" WeatherType
7274
GWeather --> "-currentWeather" WeatherType
7375
Weather --> "-observers" WeatherObserver
74-
Hobbits ..|> WeatherObserver
75-
Orcs ..|> WeatherObserver
76-
GHobbits ..|> Race
77-
GOrcs ..|> Race
78-
GWeather --|> Observable
79-
Race --|> Observer
80-
@enduml
76+
Hobbits ..|> WeatherObserver
77+
Orcs ..|> WeatherObserver
78+
GHobbits ..|> Race
79+
GOrcs ..|> Race
80+
GWeather --|> Observable
81+
Race --|> Observer
82+
@enduml
102 KB
Loading

observer/src/main/java/com/iluwatar/observer/Hobbits.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,6 @@ public class Hobbits implements WeatherObserver {
3535

3636
@Override
3737
public void update(WeatherType currentWeather) {
38-
switch (currentWeather) {
39-
case COLD:
40-
LOGGER.info("The hobbits are shivering in the cold weather.");
41-
break;
42-
case RAINY:
43-
LOGGER.info("The hobbits look for cover from the rain.");
44-
break;
45-
case SUNNY:
46-
LOGGER.info("The happy hobbits bade in the warm sun.");
47-
break;
48-
case WINDY:
49-
LOGGER.info("The hobbits hold their hats tightly in the windy weather.");
50-
break;
51-
default:
52-
break;
53-
}
38+
LOGGER.info("The hobbits are facing " + currentWeather.getDescription() + " weather now");
5439
}
5540
}

observer/src/main/java/com/iluwatar/observer/Orcs.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,6 @@ public class Orcs implements WeatherObserver {
3535

3636
@Override
3737
public void update(WeatherType currentWeather) {
38-
switch (currentWeather) {
39-
case COLD:
40-
LOGGER.info("The orcs are freezing cold.");
41-
break;
42-
case RAINY:
43-
LOGGER.info("The orcs are dripping wet.");
44-
break;
45-
case SUNNY:
46-
LOGGER.info("The sun hurts the orcs' eyes.");
47-
break;
48-
case WINDY:
49-
LOGGER.info("The orc smell almost vanishes in the wind.");
50-
break;
51-
default:
52-
break;
53-
}
38+
LOGGER.info("The orcs are facing " + currentWeather.getDescription() + " weather now");
5439
}
5540
}

observer/src/main/java/com/iluwatar/observer/WeatherType.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,20 @@
2828
*/
2929
public enum WeatherType {
3030

31-
SUNNY, RAINY, WINDY, COLD;
31+
SUNNY("Sunny"),
32+
RAINY("Rainy"),
33+
WINDY("Windy"),
34+
COLD("Cold");
35+
36+
private final String description;
37+
38+
WeatherType(String description) {
39+
this.description = description;
40+
}
41+
42+
public String getDescription() {
43+
return this.description;
44+
}
3245

3346
@Override
3447
public String toString() {

observer/src/main/java/com/iluwatar/observer/generic/GHobbits.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,6 @@ public class GHobbits implements Race {
3636

3737
@Override
3838
public void update(GWeather weather, WeatherType weatherType) {
39-
switch (weatherType) {
40-
case COLD:
41-
LOGGER.info("The hobbits are shivering in the cold weather.");
42-
break;
43-
case RAINY:
44-
LOGGER.info("The hobbits look for cover from the rain.");
45-
break;
46-
case SUNNY:
47-
LOGGER.info("The happy hobbits bade in the warm sun.");
48-
break;
49-
case WINDY:
50-
LOGGER.info("The hobbits hold their hats tightly in the windy weather.");
51-
break;
52-
default:
53-
break;
54-
}
39+
LOGGER.info("The hobbits are facing " + weatherType.getDescription() + " weather now");
5540
}
5641
}

observer/src/main/java/com/iluwatar/observer/generic/GOrcs.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,6 @@ public class GOrcs implements Race {
3636

3737
@Override
3838
public void update(GWeather weather, WeatherType weatherType) {
39-
switch (weatherType) {
40-
case COLD:
41-
LOGGER.info("The orcs are freezing cold.");
42-
break;
43-
case RAINY:
44-
LOGGER.info("The orcs are dripping wet.");
45-
break;
46-
case SUNNY:
47-
LOGGER.info("The sun hurts the orcs' eyes.");
48-
break;
49-
case WINDY:
50-
LOGGER.info("The orc smell almost vanishes in the wind.");
51-
break;
52-
default:
53-
break;
54-
}
39+
LOGGER.info("The orcs are facing " + weatherType.getDescription() + " weather now");
5540
}
5641
}

observer/src/test/java/com/iluwatar/observer/HobbitsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ public class HobbitsTest extends WeatherObserverTest<Hobbits> {
3636
@Override
3737
public Collection<Object[]> dataProvider() {
3838
return List.of(
39-
new Object[]{WeatherType.SUNNY, "The happy hobbits bade in the warm sun."},
40-
new Object[]{WeatherType.RAINY, "The hobbits look for cover from the rain."},
41-
new Object[]{WeatherType.WINDY, "The hobbits hold their hats tightly in the windy weather."},
42-
new Object[]{WeatherType.COLD, "The hobbits are shivering in the cold weather."});
39+
new Object[]{WeatherType.SUNNY, "The hobbits are facing Sunny weather now"},
40+
new Object[]{WeatherType.RAINY, "The hobbits are facing Rainy weather now"},
41+
new Object[]{WeatherType.WINDY, "The hobbits are facing Windy weather now"},
42+
new Object[]{WeatherType.COLD, "The hobbits are facing Cold weather now"});
4343
}
4444

4545
/**

observer/src/test/java/com/iluwatar/observer/OrcsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ public class OrcsTest extends WeatherObserverTest<Orcs> {
3636
@Override
3737
public Collection<Object[]> dataProvider() {
3838
return List.of(
39-
new Object[]{WeatherType.SUNNY, "The sun hurts the orcs' eyes."},
40-
new Object[]{WeatherType.RAINY, "The orcs are dripping wet."},
41-
new Object[]{WeatherType.WINDY, "The orc smell almost vanishes in the wind."},
42-
new Object[]{WeatherType.COLD, "The orcs are freezing cold."});
39+
new Object[]{WeatherType.SUNNY, "The orcs are facing Sunny weather now"},
40+
new Object[]{WeatherType.RAINY, "The orcs are facing Rainy weather now"},
41+
new Object[]{WeatherType.WINDY, "The orcs are facing Windy weather now"},
42+
new Object[]{WeatherType.COLD, "The orcs are facing Cold weather now"});
4343
}
4444

4545
/**

observer/src/test/java/com/iluwatar/observer/generic/GHobbitsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public class GHobbitsTest extends ObserverTest<GHobbits> {
3838
@Override
3939
public Collection<Object[]> dataProvider() {
4040
return List.of(
41-
new Object[]{WeatherType.SUNNY, "The happy hobbits bade in the warm sun."},
42-
new Object[]{WeatherType.RAINY, "The hobbits look for cover from the rain."},
43-
new Object[]{WeatherType.WINDY, "The hobbits hold their hats tightly in the windy weather."},
44-
new Object[]{WeatherType.COLD, "The hobbits are shivering in the cold weather."}
41+
new Object[]{WeatherType.SUNNY, "The hobbits are facing Sunny weather now"},
42+
new Object[]{WeatherType.RAINY, "The hobbits are facing Rainy weather now"},
43+
new Object[]{WeatherType.WINDY, "The hobbits are facing Windy weather now"},
44+
new Object[]{WeatherType.COLD, "The hobbits are facing Cold weather now"}
4545
);
4646
}
4747

observer/src/test/java/com/iluwatar/observer/generic/OrcsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public class OrcsTest extends ObserverTest<GOrcs> {
3838
@Override
3939
public Collection<Object[]> dataProvider() {
4040
return List.of(
41-
new Object[]{WeatherType.SUNNY, "The sun hurts the orcs' eyes."},
42-
new Object[]{WeatherType.RAINY, "The orcs are dripping wet."},
43-
new Object[]{WeatherType.WINDY, "The orc smell almost vanishes in the wind."},
44-
new Object[]{WeatherType.COLD, "The orcs are freezing cold."}
41+
new Object[]{WeatherType.SUNNY, "The orcs are facing Sunny weather now"},
42+
new Object[]{WeatherType.RAINY, "The orcs are facing Rainy weather now"},
43+
new Object[]{WeatherType.WINDY, "The orcs are facing Windy weather now"},
44+
new Object[]{WeatherType.COLD, "The orcs are facing Cold weather now"}
4545
);
4646
}
4747

0 commit comments

Comments
 (0)