模式说明
装饰者模式装饰者模式可以动态地给一个对象增加一些额外的职责。就增加功能来说,装饰者模式相比生成子类更为灵活。
模式结构图
程序示例
说明:一辆车,装饰窗户,装饰轮子
代码:
class Car(object): def description(self): print 'basic car' class WindowDecorator(Car): def __init__(self, car): self._car = car def description(self): self._car.description() print 'decorator window' class WheelDecorator(Car): def __init__(self, car): self._car = car def description(self): self._car.description() print 'decorator whell' if __name__=='__main__': car = WheelDecorator(WindowDecorator(Car())) car.description()
运行结果:
参考来源:
http://www.cnblogs.com/chenssy/p/3679190.html
http://www.cnblogs.com/wuyuegb2312/archive/2013/04/09/3008320.html
http://www.cnblogs.com/Terrylee/archive/2006/07/17/334911.html
http://www.cnblogs.com/saville/archive/2011/07/19/2110830.html