You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: observer/README.md
+13-42Lines changed: 13 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,18 +13,18 @@ tags:
13
13
Dependents, Publish-Subscribe
14
14
15
15
## 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
17
17
and updated automatically.
18
18
19
19
## Explanation
20
20
21
21
Real world example
22
22
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.
24
24
25
25
In plain words
26
26
27
-
> Register as an observer to receive state changes in the object.
27
+
> Register as an observer to receive state changes in the object.
28
28
29
29
Wikipedia says
30
30
@@ -46,22 +46,7 @@ public class Orcs implements WeatherObserver {
46
46
47
47
@Override
48
48
publicvoidupdate(WeatherTypecurrentWeather) {
49
-
switch (currentWeather) {
50
-
caseCOLD:
51
-
LOGGER.info("The orcs are freezing cold.");
52
-
break;
53
-
caseRAINY:
54
-
LOGGER.info("The orcs are dripping wet.");
55
-
break;
56
-
caseSUNNY:
57
-
LOGGER.info("The sun hurts the orcs' eyes.");
58
-
break;
59
-
caseWINDY:
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");
65
50
}
66
51
}
67
52
@@ -72,21 +57,7 @@ public class Hobbits implements WeatherObserver {
72
57
@Override
73
58
publicvoidupdate(WeatherTypecurrentWeather) {
74
59
switch (currentWeather) {
75
-
caseCOLD:
76
-
LOGGER.info("The hobbits are shivering in the cold weather.");
77
-
break;
78
-
caseRAINY:
79
-
LOGGER.info("The hobbits look for cover from the rain.");
80
-
break;
81
-
caseSUNNY:
82
-
LOGGER.info("The happy hobbits bade in the warm sun.");
83
-
break;
84
-
caseWINDY:
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");
90
61
}
91
62
}
92
63
```
@@ -141,20 +112,20 @@ Here's the full example in action.
141
112
142
113
weather.timePasses();
143
114
// 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
146
117
weather.timePasses();
147
118
// 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.
0 commit comments