-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchainOfResponsibility.py
More file actions
59 lines (41 loc) · 1.5 KB
/
chainOfResponsibility.py
File metadata and controls
59 lines (41 loc) · 1.5 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from abc import ABC, abstractmethod
from math import floor
class Handler(ABC):
@abstractmethod
def set_next(self):
pass
@abstractmethod
def handle(self):
pass
class AbstractHandler(Handler):
_next_handler: Handler = None
def set_next(self, handler: Handler) -> Handler:
self._next_handler = handler
return handler
@abstractmethod
def handle(self, request: str) -> str:
if self._next_handler:
return self._next_handler.handle(request)
return None
class Dispenser10(AbstractHandler):
def handle(self, request: int) -> str:
dispensingCnt = 0
if request >= 10:
dispensingCnt = floor(request/10)
print("Dispenser10 : {} £10 note".format(dispensingCnt))
remaining = request - (10*dispensingCnt)
if remaining > 0:
print("Dispenser10 : Hey, I can't handle £{}".format(remaining))
return super().handle(remaining)
class Dispenser5(AbstractHandler):
def handle(self, request: int) -> str:
dispensingCnt = 0
if request >= 5:
dispensingCnt = floor(request/5)
print("Dispenser20 : {} £5 note".format(dispensingCnt))
remaining = request - (5*dispensingCnt)
if remaining > 0:
print("Dispenser20 : Hey, I can't handle £{}".format(remaining))
disp10 = Dispenser10()
disp10.set_next(Dispenser5())
disp10.handle(15)