标签:console orb images 抽象 add bst png 装饰模式 main
abstract class Component{ public abstract void Operation(); } class ConcreteComponent extend Component{ public Operation(){ console.log(“具体对象操作”); } } abstract class Decorator extend Component{ protected Component component; public setComponent(Component component){ this.component = component; } public Operation(){ if(this.component != null){ component.Operation(); } } } class ConcreteDecoratorA extend Decorator{ private string addedState; public Operation(){ super.Operation(); addedState = "new State"; console.log("具体装饰对象A操作"); } } class ConcreteDecoratorB extend Decorator{ public Operation(){ super.Operation(); addedBehavior(); console.log("装饰对象B的操作"); } private addedBehavior(){} } class Main{ ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); d1.setComponent(c); d2.setComponent(d1); d2.Operation(); }
该模式利用setComponent来对对象进行包装,每个装饰对象的实现就和如何使用这个对象分离开了,每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链当中。
标签:console orb images 抽象 add bst png 装饰模式 main
原文地址:http://www.cnblogs.com/xiaohaoxuezhang/p/6768432.html