-
Notifications
You must be signed in to change notification settings - Fork 0
/
python.py
39 lines (25 loc) · 968 Bytes
/
python.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
# Decorator: Añade responsabilidades adicionales a un objeto de manera dinámica.
class Component(ABC):
@abstractmethod
def operation(self) -> str:
pass
class ConcreteComponent(Component):
def operation(self) -> str:
return "ConcreteComponent Operation"
class Decorator(Component):
def __init__(self, component: Component):
self.component = component
def operation(self) -> str:
return self.component.operation()
class ConcreteDecoratorA(Decorator):
def operation(self) -> str:
return f"ConcreteDecoratorA({super().operation()})"
class ConcreteDecoratorB(Decorator):
def operation(self) -> str:
return f"ConcreteDecoratorB({super().operation()})"
component = ConcreteComponent()
decorator_a = ConcreteDecoratorA(component)
decorator_b = ConcreteDecoratorB(decorator_a)
print(
decorator_b.operation()
) # ConcreteDecoratorB(ConcreteDecoratorA(ConcreteComponent Operation))