forked from Sean-Bradley/Design-Patterns-In-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorator_concept.py
More file actions
37 lines (26 loc) · 850 Bytes
/
decorator_concept.py
File metadata and controls
37 lines (26 loc) · 850 Bytes
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
# pylint: disable=too-few-public-methods
"Decorator Concept Sample Code"
from abc import ABCMeta, abstractmethod
class IComponent(metaclass=ABCMeta):
"Methods the component must implement"
@staticmethod
@abstractmethod
def method():
"A method to implement"
class Component(IComponent):
"A component that can be decorated or not"
def method(self):
"An example method"
return "Component Method"
class Decorator(IComponent):
"The Decorator also implements the IComponent"
def __init__(self, obj):
"Set a reference to the decorated object"
self.object = obj
def method(self):
"A method to implement"
return f"Decorator Method({self.object.method()})"
# The Client
COMPONENT = Component()
print(COMPONENT.method())
print(Decorator(COMPONENT).method())