forked from qinshuang/design_model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03装饰模式.py
161 lines (121 loc) · 5.36 KB
/
03装饰模式.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#coding:utf-8
# 设计模式
# 第三章 装饰模式
# 动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。
'''
UML类图
Component是定义一个对象接口,
可以给这些对象动态地添加职责
/
----------------
| Component |<————————————.
|+Operation() | |
---------------- |
△ |
| |-component
.——————————————————————————. |
| | |
--------------------- --------------- | Decorator,装饰抽象类,
| ConcreteComponent | | Decorator | | 继承了Component,从外类
|+Operation() | |+Operation() |◇ 来扩展Component类的功能,
--------------------- --------------- 但对于Component来说,是
△ 无需知道Decorator的存在的
|
.—————————————————————————.
| |
---------------------- ----------------------
| ConcreteDecoratorA | | ConcreteDecoratorB |
|-addedState: string | |-AddedBehavior() |
|+Operation() | |+Operation() |
---------------------- ----------------------
\ /
ConcreteDecorator就是具体的装饰对象,
起到给Component添加职责的功能
ConcreteComponent:定义了一个具体的对象,也可以给这个对象添加一些职责
'''
import abc
class Component(metaclass=abc.ABCMeta):
def __init__(self): raise
@abc.abstractmethod
def Operation(self): return
class ConcreteComponent(Component):
def __init__(self): return
def Operation(self):
print('具体对象的操作')
class Decorator(Component):
def __init__(self):
self.component = None
def SetComponent(self, component): # 设置Component
self.component = component
def Operation(self):
# 重写Operation(),实际执行的是Component的Operation()
if self.component:
self.component.Operation()
class ConcreteDecoratorA(Decorator):
def __init__(self):
# 本类特有功能,以区别于ConcreteDecoratorB
self.addedState = ''
def Operation(self):
# 首先运行原Component的Operation(),再执行本类的功能,如addedState,相当于
# 对原Component进行了装饰
super(ConcreteDecoratorA, self).Operation()
self.addedState = 'New State'
print('具体装饰对象A的操作')
class ConcreteDecoratorB(Decorator):
def __init__(self): return
def Operation(self):
# 首先运行原Component的Operation(),再执行本类的功能,如AddedBehavior(),
# 相当于对原Component进行了装饰
super(ConcreteDecoratorB, self).Operation()
self.AddedBehavior()
print('具体装饰对象B的操作')
# 本类独有方法,以区别于ConcreteDecoratorA
def AddedBehavior(self): return
def run():
c = ConcreteComponent()
d1 = ConcreteDecoratorA()
d2 = ConcreteDecoratorB()
# 装饰的方法是:首先用ConcreteComponent实例化对象c,然后用ConcreteDecoratorA的
# 实例化对象d1来包装c,再用ConcreteDecoratorB的对象d2包装d1,最终执行d2的Operation()
d1.SetComponent(c)
d2.SetComponent(d1)
d2.Operation()
# 如果只有一个ConcreteDecorator类而没有抽象的Component类,那么Decorator类可以是
# ConcreteDecorator的一个子类。同样道理,如果只有一个ConcreteDecorator类,那么就没有
# 必要建立一个单独的Decorator类,而可以把Decorator和ConcreteDecorator的责任合并成一个类。
#===============================================================================
class Person(object):
def __init__(self, name):
self.name = name
def show(self):
print('装扮的:%s' % self.name)
class Finery(Person):
def decorate(self, person):
self.person = person
def show(self):
if self.person:
self.person.show()
class TShirts(Finery):
def show(self):
print(self.name)
self.person.show()
class BigTrouser(Finery):
def show(self):
print(self.name)
self.person.show()
class S(Finery):
def show(self):
print(self.name)
self.person.show()
def main():
person = Person('大虾')
a = TShirts('T恤')
b = BigTrouser('裤子')
c = S('鞋子')
a.decorate(person)
b.decorate(a)
c.decorate(b)
c.show()
if __name__ == '__main__':
# run()
main()