forked from gennad/Design-Patterns-in-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chainofresp.py
44 lines (32 loc) · 844 Bytes
/
chainofresp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Car:
def __init__(self):
self.name = None
self.km = 11100
self.fuel = 5
self.oil = 5
def handle_fuel(car):
if car.fuel < 10:
print "added fuel"
car.fuel = 100
def handle_km(car):
if car.km > 10000:
print "made a car test."
car.km = 0
def handle_oil(car):
if car.oil < 10:
print "Added oil"
car.oil = 100
class Garage:
def __init__(self):
self.handlers = []
def add_handler(self, handler):
self.handlers.append(handler)
def handle_car(self, car):
for handler in self.handlers:
handler(car)
if __name__ == '__main__':
handlers = [handle_fuel, handle_km, handle_oil]
garage = Garage()
for handle in handlers:
garage.add_handler(handle)
garage.handle_car(Car())