相较于建造者模式,建造者模式要求建造的过程必须是稳定的,装饰模式的建造过程不稳定的。
概念:装饰模式是利用SetComponent来对对象进行包装的,每个装饰对象的实现就和如何使用这个对象分离开了,每个装饰对象之关心自己的功能,不需要关心如何添加到对象链中
核心:为已有功能动态地添加更多功能的一种方式
装饰模式结构图

如果只有一个ConcreteComponent类而没有抽象的Component类,可以让Decorator直接继承ConcreteComponent。
如果只有一个ConcreteDecorator类,没有Decorator类,可以把ConcreteComponent和Decorator合成一个类
场景:一个人搭配不同衣服出门

java代码:
public class DecoratorTest {public static void main(String[] args) {Person person = new Person("涛涛");TShirts tShirts = new TShirts();BigTrouser bigTrouser = new BigTrouser();//装饰步骤tShirts.Decorate(person);bigTrouser.Decorate(tShirts);bigTrouser.show();}}public class Person {public Person(){}String name;public Person(String name){this.name = name;}public void show(){System.out.println("装扮的" + name);}}//Decorator装饰器public class Finery extends Person{protected Person component;public void Decorate(Person person){component = person;}public void show(){if (component != null){component.show();}}}public class BigTrouser extends Finery{@Overridepublic void show() {System.out.print("垮裤 ");super.show();}}public class TShirts extends Finery {@Overridepublic void show() {System.out.print("穿T恤 ");super.show();}}
