您的位置:首页 > 编程语言 > Python开发

Python设计模式——装饰模式(Decorator)

2014-07-30 10:47 477 查看
假如我们需要开发一个程序来展示一个人穿衣服的过程。

#encoding=utf-8
__author__ = 'kevinlu1010@qq.com'
class Person():
def __init__(self,name):
print '%s开始穿衣'%name
def wear_tshirt(self):
print '穿TShirst'
def wear_trouser(self):
print '穿裤子'
def wear_shoe(self):
print '穿T鞋子'
def wear_tie(self):
print '穿领带'

if __name__=='__main__':
person=Person('kevin')
person.wear_shoe()
person.wear_tie()
person.wear_trouser()


这样写无疑是最快的,代码最简洁的,但是扩展性比较差,例如客户要求我们增加一个穿袜子的动作,我们就需要修改Person类,但是根据封闭-开发原则中的封闭原则,一个类写完之后是尽量不要修改它的,所以我们就需要另外一种实现方式

#encoding=utf-8
__author__ = 'kevinlu1010@qq.com'

from abc import ABCMeta, abstractmethod
class Person():
def __init__(self, name):
print '%s开始穿衣' % name

class Finery():
__metaclass__ = ABCMeta
@abstractmethod
def show(self):
pass
class TShirt(Finery):
def show(self):
print '穿TShirst'

class Trouser(Finery):
def show(self):
print '穿裤子'

class Shoe(Finery):
def show(self):
print '穿鞋子'

class Tie(Finery):
def show(self):
print '穿领带'

if __name__ == '__main__':
person = Person('kevin')
finerys=[]
finerys.append(TShirt())
finerys.append(Trouser())
finerys.append(Shoe())
finerys.append(Tie())
map(lambda x:x.show(),finerys)


首先定义一个积累Finery,定义一个抽象方法show,然后每一个穿衣动作都写一个类,重写show方法。

如果客户修改需求,我们就新增加一个类就可以了。

装饰模式的做法:

#encoding=utf-8
__author__ = 'kevinlu1010@qq.com'

from abc import ABCMeta, abstractmethod

class Person():
def __init__(self, name):
self.name = name

def decorator(self, component):
self.component = component

def show(self):
print '%s开始穿衣' % self.name
self.component.show()

class Finery():
def __init__(self):
self.component = None

def decorator(self, component):
self.component = component

__metaclass__ = ABCMeta

@abstractmethod
def show(self):
if self.component:
self.component.show()

class TShirt(Finery):
def show(self):
Finery.show(self)
print '穿TShirst'

class Trouser(Finery):
def show(self):
Finery.show(self)
print '穿裤子'

class Shoe(Finery):
def show(self):
Finery.show(self)
print '穿鞋子'

class Tie(Finery):
def show(self):
Finery.show(self)
print '穿领带'

if __name__ == '__main__':
person = Person('kevin')
tshirt = TShirt()
trouser = Trouser()
shoe = Shoe()
tie = Tie()

trouser.decorator(tshirt)
shoe.decorator(trouser)
tie.decorator(shoe)
person.decorator(tie)
person.show()


每个类都有show方法,衣服类都有decorator方法,利用这个方法,动态地把不同衣服的show方法装饰到person这个类上,这样做一方面可以令person类更为精简,因为在实际应用中Person类可能会有很多方法,而穿衣服这个需求只是其中一个,另一方面是,增加Person类的可扩展性,例如如果Person类已经写好了,现在新的需求需要在某一次调用Person类的show方法的时候增加穿衣服的功能,这种模式就能很好地实现了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: