-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecorator_pattern.py
74 lines (54 loc) · 1.26 KB
/
decorator_pattern.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'Andy'
"""
大话设计模式
设计模式——装饰模式
装饰模式(Decorator Pattern):动态的给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活.
特点: 有效的把类的核心职责和装饰功能区分开,而且可以去除相关类中重复的装饰逻辑
"""
# 定义对象接口
class Person(object):
def __init__(self,name):
self.name = name
def show(self):
print "装扮的%s"%self.name
#装饰类
class Finery(Person):
def __init__(self):
pass
def Decorate(self,componet):
self.componet = componet
def show(self):
if self.componet != None:
self.componet.show()
#装扮——T恤
class TShirts(Finery):
def __init__(self):
pass
def show(self):
print 'T恤'
self.componet.show()
#装扮——大裤衩
class BigTrouser(Finery):
def __init__(self):
pass
def show(self):
print '大裤衩'
self.componet.show()
# 装扮——人字拖
class FlipFlops(Finery):
def __init__(self):
pass
def show(self):
print '人字拖'
self.componet.show()
if __name__ == '__main__':
p = Person('Andy')
ff = FlipFlops()
bt = BigTrouser()
ts = TShirts()
ff.Decorate(p)
bt.Decorate(ff)
ts.Decorate(bt)
ts.show()