-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathDecoratorPattern.py
55 lines (36 loc) · 1004 Bytes
/
DecoratorPattern.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
# coding: utf8
import abc
class Component(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def process(self):
pass
class ConcreteComponent(Component):
def process(self):
print("process in ConcreteComponent")
class Decorator(Component):
def __init__(self, component):
self._component = component
def process(self):
self.before()
self._component.process()
self.after()
@abc.abstractmethod
def before(self):
pass
@abc.abstractmethod
def after(self):
pass
class LogDecorator(Decorator):
def before(self):
print("before calling process")
def after(self):
print("after calling process")
if __name__ == "__main__":
import unittest
class DecoratorTest(unittest.TestCase):
def testDecorator(self):
component = ConcreteComponent()
decorator = LogDecorator(component)
decorator.process()
unittest.main()