Loading... # 设计模式(四)——装饰模式 ## 参考 > 大话设计模式  ——  程杰 著 ## 目录 [设计模式(一)——简单工厂模式](https://www.princelei.club/archives/67.html) [设计模式(二)——策略模式](https://www.princelei.club/archives/68.html) [设计模式(三)——设计原则](https://www.princelei.club/archives/116.html) [设计模式(四)——装饰模式](https://www.princelei.club/archives/117.html) [设计模式(五)——代理模式](https://www.princelei.club/archives/119.html) [设计模式(六)——工厂方法模式](https://www.princelei.club/archives/132.html) [设计模式(七)——原型模式](https://www.princelei.club/archives/133.html) [设计模式(八)——模板方法模式](https://www.princelei.club/archives/134.html) [设计模式(九)——外观模式](https://www.princelei.club/archives/135.html) [设计模式(十)——建造者模式](https://www.princelei.club/archives/136.html) [设计模式(十一)——观察者模式](https://www.princelei.club/archives/137.html) [设计模式(十二)——抽象工厂模式](https://www.princelei.club/archives/138.html) [设计模式(十三)——状态模式](https://www.princelei.club/archives/139.html) [设计模式(十四)——适配器模式](https://www.princelei.club/archives/140.html) [设计模式(十五)——备忘录模式](https://www.princelei.club/archives/141.html) [设计模式(十六)——组合模式](https://www.princelei.club/archives/147.html) [设计模式(十七)——迭代器模式](https://www.princelei.club/archives/148.html) [设计模式(十八)——单例模式](https://www.princelei.club/archives/157.html) [设计模式(十九)——桥接模式](https://www.princelei.club/archives/159.html) [设计模式(二十)——命令模式](https://www.princelei.club/archives/160.html) [设计模式(二十一)——职责链模式](https://www.princelei.club/archives/161.html) [设计模式(二十二)——中介者模式](https://www.princelei.club/archives/162.html) [设计模式(二十三)——享元模式](https://www.princelei.club/archives/163.html) [设计模式(二十四)——解释器模式](https://www.princelei.club/archives/173.html) [设计模式(二十五)——访问者模式](https://www.princelei.club/archives/174.html) ## 何为装饰模式 - 装饰模式(Decorator),动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。   装饰模式是为已有功能动态地添加更多功能的一种方式,当系统需要新功能的时候,是向旧的类中添加新的代码。这些新加代码通常装饰了原有类的核心职责或主要行为,在主类中加入新的字段,新的方法和新的逻辑,从而增加了主类的复杂度,而这些新加入的东西仅仅是为了满足一些只在某种特定情况下才会执行的特殊行为的需要。装饰模式却提供了一个非常好的解决方案,它把每个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象,因此,当需要执行特殊行为时,客户代码就可以在运行时根据需要有选择地、按顺序地使用装饰功能包装对象了。   装饰模式的优点就是,把类中的装饰功能从类中搬移去除,这样可以简化原有的类,有效地把类的核心职责和装饰功能区分开了。而且可以去除相关类中重复的装饰逻辑。 ## 用装饰模式实现给一个人穿不同的服装 ``` /** * 抽象类 */ public interface Person { void show(); } ``` ``` /** * Person类 */ public class ThisPerson implements Person { private String name; public ThisPerson(String name) { this.name = name; } @Override public void show() { System.out.println("装饰的" + name); } } ``` ``` /** * 服装抽象类 */ public abstract class Finery implements Person { public abstract void show(); } ``` 具体服装类 ``` /** * 破球鞋 */ public class Sneakers extends Finery { private Person component; public Sneakers(Person component) { this.component = component; } @Override public void show() { if (component != null) { component.show(); System.out.println("破球鞋"); } } } ``` ``` /** * 大T恤 */ public class TShirts extends Finery{ private Person component; public TShirts(Person component) { this.component = component; } @Override public void show() { if (component != null) { component.show(); System.out.println("大T恤"); } } } ``` 有多少种服装就添加多少个装饰类。其余服装类似,省略…… ``` /** * 客户端程序 */ public class DecoratorMainClass { public static void main(String[] args) { Person person = new ThisPerson("Tom"); person = new Sneakers(person); person = new BigTrouser(person); person = new TShirts(person); person.show(); person = new ThisPerson("Jerry"); person = new LeatherShoes(person); person = new Tie(person); person = new Suit(person); person.show(); } } ``` 输出结果: ``` 装饰的Tom 破球鞋 垮裤 大T恤 装饰的Jerry 皮鞋 领带 西装 ``` 是不是觉得这种使用方式有点眼熟?没发觉的话可以把new实例的写法转换成这样: ``` Person person = new Sneakers(new BigTrouser(new TShirts(new ThisPerson("Tom")))); ``` 这是不是特别像我们创建io流时,用高级流装饰低级流,没错,创建高级流就是一个给低级流加装饰器的过程。 Last modification:June 11th, 2020 at 06:27 pm © 允许规范转载
哈哈哈,写的太好了https://www.lawjida.com/
这是一条测试评论