This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
decorator.py
53 lines (37 loc) · 1.44 KB
/
decorator.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
"""Decorator pattern
Decorator is a structural design pattern. It is used to extend (decorate) the
functionality of a certain object at run-time, independently of other instances
of the same class.
The decorator pattern is an alternative to subclassing. Subclassing adds
behavior at compile time, and the change affects all instances of the original
class; decorating can provide new behavior at run-time for selective objects.
"""
class Component(object):
# method we want to decorate
def whoami(self):
print("I am {}".format(id(self)))
# method we don't want to decorate
def another_method(self):
print("I am just another method of {}".format(self.__class__.__name__))
class ComponentDecorator(object):
def __init__(self, decoratee):
self._decoratee = decoratee # reference of the original object
def whoami(self):
print("start of decorated method")
self._decoratee.whoami()
print("end of decorated method")
# forward all "Component" methods we don't want to decorate to the
# "Component" pointer
def __getattr__(self, name):
return getattr(self._decoratee, name)
def main():
a = Component() # original object
b = ComponentDecorator(a) # decorate the original object at run-time
print("Original object")
a.whoami()
a.another_method()
print("\nDecorated object")
b.whoami()
b.another_method()
if __name__ == "__main__":
main()