-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorator_main.py
More file actions
50 lines (34 loc) · 1.22 KB
/
decorator_main.py
File metadata and controls
50 lines (34 loc) · 1.22 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
class Component:
def operation(self) -> str:
# this operation can be altered by decorators
pass
class ConcreteComponent(Component):
def operation(self) -> str:
return "This is a concrete component"
class Decorator(Component):
_component: Component = None
def __init__(self, component: Component) -> None:
self._component = component
@property
def component(self) -> str:
return self._component
def operation(self) -> str:
return self._component.operation()
class ConcreteDecoratorA(Decorator):
def operation(self) -> str:
return f"ConcreteDecoratorA({self.component.operation()})"
class ConcreteDecoratorB(Decorator):
def operation(self) -> str:
return f"ConcreteDecoratorB({self.component.operation()})"
def client_code(component: Component) -> None:
print(f"Result : {component.operation()}")
print("\n")
if __name__ == "__main__":
simple = ConcreteComponent()
print("Client : I have a simple component-")
client_code(simple)
print("\n")
decorator1 = ConcreteDecoratorA(simple)
decorator2 = ConcreteDecoratorB(decorator1)
print("Client : I now have decorated component-")
client_code(decorator2)