装饰模式又叫包装模式,装饰模式以客户端透明的方式扩展对象的功能,是继承关系的一个替代方案.装饰模式可以在不使用创造更多的子类的情况下,将对象的功能加以扩展. 装饰模式结构图如下: 其包含的角色就分为: 抽象构建角色Component:给出一个抽象结构,以规范准备接受附加责任的对象. 具体构建角色Concrete Component:定义一个将要接受附加责任的类. 装饰角色Decorator:持有一个构建Component对象的实例,并定义一个与抽象构建一直的接口 具体装饰角色Concrete D…
Python版 https://github.com/faif/python-patterns/blob/master/structural/decorator.py #!/usr/bin/env python # -*- coding: utf-8 -*- """ *What is this pattern about? The Decorator pattern is used to dynamically add a new feature to an object w…
public class Client { public static void main(String[] args) { Component component=new ConcreteComponent(); component=new ConcreteDecoratorA(component); component=new ConcreteDecoratorB(component); component.doSth(); } } interface Component{ void doS…
Python版 https://github.com/faif/python-patterns/blob/master/structural/proxy.py #!/usr/bin/env python # -*- coding: utf-8 -*- """ *TL;DR80 Provides an interface to resource that is expensive to duplicate. """ from __future__…
Python版 https://github.com/faif/python-patterns/blob/master/structural/mvc.py #!/usr/bin/env python # -*- coding: utf-8 -*- """ *TL;DR80 Separates data in GUIs from the ways it is presented, and accepted. """ class Model(obje…
Python版 https://github.com/faif/python-patterns/blob/master/structural/front_controller.py #!/usr/bin/env python # -*- coding: utf-8 -*- """ @author: Gordeev Andrey <gordeev.and.and@gmail.com> *TL;DR80 Provides a centralized entry poi…
Python版 https://github.com/faif/python-patterns/blob/master/structural/flyweight.py #!/usr/bin/env python # -*- coding: utf-8 -*- """ *References: http://codesnipers.com/?q=python-flyweights *TL;DR80 Minimizes memory usage by sharing data w…
Python版 https://github.com/faif/python-patterns/blob/master/structural/facade.py #!/usr/bin/env python # -*- coding: utf-8 -*- """ *What is this pattern about? The Facade pattern is a way to provide a simpler unified interface to a more com…
Python版 https://github.com/faif/python-patterns/blob/master/structural/composite.py #!/usr/bin/env python # -*- coding: utf-8 -*- """ *What is this pattern about? The composite pattern describes a group of objects that is treated the same w…
Python版 https://github.com/faif/python-patterns/blob/master/structural/bridge.py #!/usr/bin/env python # -*- coding: utf-8 -*- """ *References: http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Bridge_Pattern#Python *TL;DR80 Dec…