国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > 装饰者模式

装饰者模式

来源:程序员人生   发布时间:2015-06-09 08:04:11 阅读次数:3043次

装潢模式:动态地给1个对象添加1些额外的职责,就增加功能而言,装潢者模式比生成子类更加灵活。Component是定义1个对象接口,可以给这些对象动态地添加职责,ConcreteComppnent是定义了1个具体的对象,也能够给这个对象添加1些职责,Decorator,装潢抽象类,继承了Component,从外类来扩大Component类的功能,但对Component来讲,是无需知道Decorator的存在的,至于ConcreteDecorator就是具体的装潢对象,起到给Component添加职责的功能




上面是装潢者模式Decorator的类图。

实现代码以下:

Component类: abstract class Component{ public abstract void operation(); } ConcreteComponent类: class ConcreteCompoent extends Component{ public void operation{ System.out.println("具体对象的操作:"); } } Decorator类: abstract class Decorator extends Compoent{ protected Component compoent; pbulic void setComponent(Component component){ this.component = component; } public void operation(){ if(component!=null){ component.Operation(); } } } class ConcreteDecortorA extends Decorator{ private String addedState; public void operation(){ super.operation(); addedState = "new Stare"; System.out.println("具体装潢对象A的操作"); } } class ConcreteDecortorB extends Decorator{ public void Operation(){ super.Operation(); AddedBehavior(); System.out.println("具体装潢对象B的操作"); } private void AddedBehavior(){ } } public class Test{ public static void main(String[] args) { ConcreteComponent c = new ConcreteComponent(); ConcreteComponentA d1 = new ConcreteComponentA(); ConcreteComponentB d2 = new ConcreteComponentB(); d1.setComponent(c); d2.setComponent(d1); d2.operation(); } }

装潢者模式是利用setComponent来对对象进行包装的,这样每一个装潢对象的实现就和如何使用这个对象分离开了,每一个装潢对象只关心自己的功能,不需要关心如何被添加到对象链当中。

如果只有1个ConcreteComponent类而没有抽象的Component类,那末Decotator类可以是ConcreteComponent的1个子类,一样道理,如果只有1个ConcreteDecorator类,那末就没有必要建立1个单独的Decorator类,而可以把Decorator和ConcreteDecorator的责任合并成1个类。

下面是1个例子:

public class Person { public Person() { } private String name; public Person(String name) { this.name = name; } public void show() { System.out.println("打扮的:" + name); } } public class Finery extends Person { protected Person component; public void Decorate(Person component) { this.component = component; } public void show() { if (component != null) { component.show(); } } } class TShirts extends Finery { public void show() { System.out.println("大T恤"); super.show(); } } class BigTrouser extends Finery { public void show() { System.out.println("垮裤"); super.show(); } } class Sneakers extends Finery { public void show() { System.out.println("破球鞋"); super.show(); } } class LeatherShoes extends Finery { public void show() { System.out.println("皮鞋"); super.show(); } } class Tie extends Finery { public void show() { System.out.println("领带"); super.show(); } } class Suit extends Finery { public void show() { System.out.println("西装"); super.show(); } } public class Test { public static void main(String[] args) { Person xc = new Person("菜鸟"); System.out.println("第1种打扮"); Sneakers pqx = new Sneakers(); BigTrouser kk = new BigTrouser(); TShirts dtx = new TShirts(); pqx.Decorate(xc); kk.Decorate(pqx); dtx.Decorate(kk); dtx.show(); } }

小结:
装潢者模式是为已有功能动态地添加更多功能的1种方式,甚么情况用到呢?当系统需要新的功能的时候,在1般情况下,我们想到的是向旧的类中添加新的代码。这些新的代码通常装潢了原有类的核心职责或主要行动,他们在主类中加入了新的字段、新的方法和新的逻辑,从而增加了主类的复杂度,而这些新加入的东西仅仅是为了满足1些只有在某种特定情况下才会履行的特殊行动的需要,而装潢者模式可以提供1个非常好的解决方案,它把每一个要装潢的功能放在单独的类中,并让这个类包装它所要装潢的对象,因此,当需要履行特殊行动时,客户代码就能够在运行时根据需要有选择地、按顺序地使用装潢功能包装对象了。装潢者模式的优点是把类中的装潢功能从类中搬移去除,这样可以简化原本的类;另外1个使有效地把类的核心职责和装潢功能辨别开了,而且可以去除相干类中重复的装潢逻辑。



生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生