扫码在手机阅读
大话设计模式
第209章
//someextrafunctionalitygoeshere
publicvoiddoSomething
addedBehavior;
//构件类
publicabstractclassComponent
publicabstractvoiddoSomething;
//具体构件类
publicclassConcreteComponentextendsComponent
super(ponent);
privatevoidaddedBehavior
}
大B:“简化必须注意两点:1、一个装饰器类的接口必须与被装饰的类的接口相容。2、尽量保持Component作为一个‘轻’类。Component类的职责在于为各个具体装饰器类提供共同的接口,而不是存储数据,所以不要把太多的逻辑和状态放在Component类里面。省略Component接口,只有一个具体的ConcreteComponent类,则Decorater经常作为ConcreteComponent的子类。”
publicvoiddoSomething
//provideimplementationhere
publicDecorator(Componentponent)
//具体装饰器类
publicclassConcreteDecoratorextendsDecorator
publicConcreteDecorator(Componentponent)
publicabstractclassDecoratorextendsComponent
protectedComponentponent;
=ponent;
publicvoiddoSomething
//装饰器抽象类