设计模式金玉良言<二>
DECORATOR PATTERN DEFINITION: it attaches additional responsibilities to an object dynamically and provide a flexible alternative to subclassing for extending functionality.(装饰模式可以动态地添加一些功能而无需改动已有的代码)Decorator Pattern(Starbuzz) Big Picture:The features of Decorator Pattern
*?Decorators have the same supertype as the objects they decorate.(装饰类和被装饰类具有同样的超类)
*?you can use one or more decorators to wrap an object.(可以用多个装饰类来包装同一个类)
*?we can pass around a decorated object in place of the original(wrapped) object.(考虑到装饰类和被装饰类具有同样的超类,我们可以嵌套装饰。??)
*?the decorator adds its own behavior either before? and/or after delegating to the object it decorates to do the rest of the job.(装饰类会调用被装饰类的behavior来完成自己的behavior)
* objects can be decorated at any time, so we can decorate objects dynamically at runtime with as many decorators as we like.(可以在任何时候来装饰对象,常用的方法是用构造函数来实现。)
FACTORY METHOD PATTERN DEFINITION: it defines an interface for creating an object, but lets subclasses decide which class to instantiate. (抽象方法工厂模式定义接口去创建对象,同时在子类中才能决定怎样实例化对象,接口本身不能决定对象实例。)FACTORY PATTERN DEFINITION: it lets a class defer instantiation to subclasses.(工厂模式只是延迟对象的实例化。)当然工厂模式都解耦了用于创建对象的类和使用对象的类的关系,而抽象方法工厂模式用继承的方法增加extensibility,避免修改已有的代码。Factory Method Pattern Big PictureDesign Principle: Depend upon abstractions, Do not depend upon concrete classes. sunds a lot like "Pramgram to an interface, not an implementation.".