-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain_of_responsibility_pattern.py
104 lines (68 loc) · 2.48 KB
/
chain_of_responsibility_pattern.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
Creates a series simple Handler type classes before reaching the final business logic layer. works like chain connected
to each other, can move further only if current handler pass you through, otherwise exit away.
..date.. March 26, 2020
..real-time eg.. you have different level of authorizations for each registered user in your app, you dont want to
let everybody access all things. Set different layer of validation layers 'Handlers' technically to
solve this.
https://refactoring.guru/design-patterns/chain-of-responsibility/python/example#lang-features
"""
from abc import ABC, abstractmethod
from typing import Any, Optional
class Handler(ABC):
@abstractmethod
def set_next(self, handler):
pass
@abstractmethod
def handle(self, request):
pass
class BaseHandler(Handler):
_instance: Handler = None
def set_next(self, handler):
self._instance = handler
if self._instance:
return self._instance
def handle(self, request):
if self._instance:
return self._instance.handle(request)
return None
class MonkeyHandler(BaseHandler):
def handle(self, request):
if request == 'banana':
return f"{self.__class__.__name__} can have the {request}"
else:
return super().handle(request)
class BirdHandler(BaseHandler):
def handle(self, request):
if request == 'seeds':
return f"{self.__class__.__name__} can have the {request}"
else:
return super().handle(request)
class CatHandler(BaseHandler):
def handle(self, request):
if request == 'milk':
return f"{self.__class__.__name__} can have the {request}"
else:
return super().handle(request)
class MouseHandler(BaseHandler):
def handle(self, request) -> str:
if request == 'cake':
return f"{self.__class__.__name__} can have the {request}"
else:
return super().handle(request)
monkey = MonkeyHandler()
cat = CatHandler()
mouse = MouseHandler()
bird = BirdHandler()
monkey.set_next(cat).set_next(bird).set_next(mouse)
def food_splitter(handler: Handler) -> None:
food = ['banana', 'milk', 'cake', 'seeds']
for _ in food:
print(f"Who wants {_}")
response = handler.handle(_)
if response:
print(response)
else:
print(f"{_} left untouched")
food_splitter(monkey)
food_splitter(cat)