Skip to content

Commit 1166dbc

Browse files
committed
added video tut for chain of responsibility, started on observer pattern
1 parent 490c359 commit 1166dbc

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-2
lines changed

chain_of_responsibility/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ The objects in the chain will decide how to process them and/or pas them to the
77
The objects can also modify the next in the chain if for example you wanted to run objects in a recursive manner.
88

99

10-
## Chain of Responsibility UML Diagram
10+
## Chain of Responsibility Diagram
1111
![Chain of Responsibility UML Diagram](chain_of_responsibility.png)
1212

1313

@@ -43,4 +43,8 @@ Dispensing 2 £50 note
4343
Dispensing 1 £20 note
4444
Dispensing 1 £10 note
4545
Go spoil yourself
46-
```
46+
```
47+
48+
## Chain of Responsibility Video Tutorial
49+
[![Chain of Responsibility Video Tutorial](https://img.youtube.com/vi/1z1Z79cOtCE/0.jpg)](https://youtu.be/1z1Z79cOtCE)
50+

observer/classes.dot

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
digraph "classes" {
2+
charset="utf-8"
3+
rankdir=BT
4+
"0" [label="{ABCWeather|\l|update()\l}", shape="record"];
5+
"1" [label="{BBCWeather|\l|update()\l}", shape="record"];
6+
"2" [label="{COLD|name\lvalue\l|}", shape="record"];
7+
"3" [label="{IObserver|\l|update()\l}", shape="record"];
8+
"4" [label="{NBCWeather|\l|update()\l}", shape="record"];
9+
"5" [label="{RAINY|name\lvalue\l|}", shape="record"];
10+
"6" [label="{SUNNY|name\lvalue\l|}", shape="record"];
11+
"7" [label="{WINDY|name\lvalue\l|}", shape="record"];
12+
"8" [label="{Weather|\l|notify()\lregister()\lremove_observer()\l}", shape="record"];
13+
"9" [label="{WeatherType|\l|}", shape="record"];
14+
"0" -> "3" [arrowhead="empty", arrowtail="none"];
15+
"1" -> "3" [arrowhead="empty", arrowtail="none"];
16+
"4" -> "3" [arrowhead="empty", arrowtail="none"];
17+
}

observer/observer.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from abc import ABCMeta, abstractstaticmethod
2+
from enum import Enum
3+
import time
4+
5+
6+
class WeatherType(Enum):
7+
SUNNY = 1
8+
RAINY = 2
9+
WINDY = 3
10+
COLD = 4
11+
12+
13+
class Weather():
14+
"""Observable"""
15+
16+
def __init__(self):
17+
self._observers = set()
18+
pass
19+
20+
def register(self, observer):
21+
self._observers.add(observer)
22+
23+
def remove_observer(self, observer):
24+
self._observers.remove(observer)
25+
26+
def notify(self, weather_type):
27+
for observer in self._observers:
28+
observer.update(weather_type)
29+
30+
31+
class IObserver(metaclass=ABCMeta):
32+
33+
@abstractstaticmethod
34+
def update(WeatherType):
35+
"""Update all the registered observers"""
36+
37+
38+
class BBCWeather(IObserver):
39+
def __init__(self, observable):
40+
observable.register(self)
41+
42+
def update(self, weather_type):
43+
print(f"{__class__} : {repr(weather_type)}")
44+
45+
46+
class ABCWeather(IObserver):
47+
def __init__(self, observable):
48+
observable.register(self)
49+
50+
def update(self, weather_type):
51+
print(f"{__class__} : {repr(weather_type)}")
52+
53+
54+
class NBCWeather(IObserver):
55+
def __init__(self, observable):
56+
observable.register(self)
57+
58+
def update(self, weather_type):
59+
print(f"{__class__} : {repr(weather_type)}")
60+
61+
62+
if __name__ == "__main__":
63+
64+
WEATHERSERVICE = Weather()
65+
66+
BBCWEATHER = BBCWeather(WEATHERSERVICE)
67+
ABCWEATHER = ABCWeather(WEATHERSERVICE)
68+
NBCWEATHER = NBCWeather(WEATHERSERVICE)
69+
70+
WEATHERSERVICE.notify(WeatherType.RAINY)
71+
WEATHERSERVICE.remove_observer(BBCWEATHER)
72+
time.sleep(2)
73+
WEATHERSERVICE.notify(WeatherType.SUNNY)
74+
WEATHERSERVICE.remove_observer(NBCWEATHER)
75+
time.sleep(2)
76+
WEATHERSERVICE.notify(WeatherType.WINDY)
77+
time.sleep(2)
78+
WEATHERSERVICE.notify(WeatherType.COLD)

0 commit comments

Comments
 (0)