设计模式(10)-------装饰者模式
来源:互联网 发布:更改mac地址 编辑:程序博客网 时间:2024/06/06 19:19
装饰者模式
1. 什么是装饰者模式
装饰者模式 好比给原来的相片添加相框。不改变原来的相片,只是在它的外面表上一相框,给相片添加某些特定的功能,比如,用来防潮等等。从类的角度看就是,不改变原有的对象,通过 组合 的方法来添加功能。注意:只要你愿意还可以在有相框的照片上再添加一相框,这里也一样给某对象装饰多个功能。
2. 如何做呢
这样的一个需求:
在一个桌面端的代码中,我事先已经实现了 Window 、TextBox 、ListBox 三个组件。现在对它们有了新的需求:
- 添加滑动轴
- 添加黑边框
一种解决方案:
我找到它们的各自的类,然后添加一些相关方法用以实现这些新的需求。或者是继承它们用子类实现新的需求。很显然,这种方式违背了:高内聚,低耦合 。还有就是 Java 不能多继承,假如我既需要添加滑动轴又想来个黑边框,那怎么办呢?
另一种解决方案:
我们使用 装饰者模式(DecoratorPattern),我们先画一个UML类图,然后再具体分析。
最顶层的是 DisplayInterface 接口,然后有四个类实现了该接口。其中,Window、TextBox 、ListBox 是已经实现好的三个组件。而 DecoratorAbstract 是一个用于装饰的抽象类,设计为抽象主要是在多个方面或多个功能上都原有类的对象进行装饰。并且,它与 DisplayInterface 接口有组合关系(核心,还要区分组合与聚合关系)。最下面的两个类很明显就是上面抽象的具体实现了,也就是具体的装饰功能实现。它们在构造的时候就需要把待装饰的类注入。它们里面还有 addBehavior 方法,该方法就是对新添加功能的描述。
通过上述的关系描述,可以看到为了实现新的需求只需添加几个类。而且若又有的新的需求,只需要继承自 DecoratorPattern 即可。还有,我们可以很容易的把几个装饰叠加(注意叠加的顺序)。Java 类库中的 IO 处理,一层套一层的不同读写方式就是利用了 装饰者模式 。
上述 UML 对应的代码
DisplayDecorator
package DesignPattern.DecoratorPattern; /** * Created by computer on 17-5-29. */ public interface DisplayInterface { void display(); }
Window
、TextBox
、ListBox
package DesignPattern.DecoratorPattern; /** * Created by computer on 17-5-29. */ public class Window implements DisplayInterface { @Override public void display() { System.out.println("I'm Window component!"); } } package DesignPattern.DecoratorPattern; /** * Created by computer on 17-5-29. */ public class TextBox implements DisplayInterface { @Override public void display() { System.out.println("I'm TextBox component!"); } } package DesignPattern.DecoratorPattern; /** * Created by computer on 17-5-29. */ public class ListBox implements DisplayInterface { @Override public void display() { System.out.println("I'm ListBox component!"); } }
DecoratorAbstract
package DesignPattern.DecoratorPattern; /** * Created by computer on 17-5-29. */ public abstract class DecoratorAbstract implements DisplayInterface { private DisplayInterface component; public DecoratorAbstract(DisplayInterface component) { this.component = component; } @Override public void display() { component.display(); } }
BlackBorderDecorator
、ScrollBarDecorator
package DesignPattern.DecoratorPattern; /** * Created by computer on 17-5-29. */ public class BlackBorderDecorator extends DecoratorAbstract { public BlackBorderDecorator(DisplayInterface component) { super(component); } private void addBehavior(){ System.out.println("我给你加一个黑边框!"); } public void display(){ super.display(); addBehavior(); } } package DesignPattern.DecoratorPattern; /** * Created by computer on 17-5-29. */ public class ScrollBarDecorator extends DecoratorAbstract { public ScrollBarDecorator(DisplayInterface component) { super(component); } private void addBehavior(){ System.out.println("我给你加个滑动轴!"); } @Override public void display() { super.display(); addBehavior(); } }
Test测试类
package DesignPattern.DecoratorPattern; /** * Created by computer on 17-5-29. * 测试类 */ public class Test { public static void main(String[] args) { BlackBorderDecorator blackBorderWindow = new BlackBorderDecorator(new Window()); blackBorderWindow.display(); ScrollBarDecorator scrollBarAndBlackBorderWindow = new ScrollBarDecorator(blackBorderWindow); scrollBarAndBlackBorderWindow.display(); ScrollBarDecorator scrollBarListBox = new ScrollBarDecorator(new ListBox()); scrollBarListBox.display(); BlackBorderDecorator blackBorderAndScrollBarListBox = new BlackBorderDecorator(scrollBarListBox); blackBorderAndScrollBarListBox.display(); } }
3. 装饰者模式更准确的定义
到这里基本的装饰者模式已经了解了,是时候看看 官方 的定义了
装饰模式(Decorator Pattern):动态地给一个对象增加一些额外的职责,就增加对象功能来说,装饰模式比生成子类实现更为灵活。装饰模式是一种对象结构型模式。
- 设计模式(10)-------装饰者模式
- 设计模式-装饰者模式
- 设计模式:装饰者模式
- 设计模式:装饰者模式
- 设计模式: 装饰者模式
- 设计模式--装饰者模式
- 设计模式-装饰者模式
- 设计模式--装饰者模式
- 设计模式-装饰者模式
- 设计模式------------装饰者模式
- 设计模式--装饰者模式
- 设计模式:装饰者模式
- 设计模式-装饰者模式
- 设计模式-装饰者模式
- 设计模式-装饰者模式
- 设计模式--装饰者模式
- 【设计模式】装饰者模式
- 设计模式---装饰者模式
- [Nmap渗透测试指南]第三章(探索网络)
- 【算法题】统计字符串单词个数
- List、Set、Map集合的解说与区别
- Python中RowIOBase详解
- lintcode(107)单词切分
- 设计模式(10)-------装饰者模式
- python学习笔记。
- Linux中的周期性计划任务
- CentOS64安装oracle服务器
- Mac x 10.12.4 如何安装sshfs
- jsp九大内置对象---pageContext
- hdu 1251 字典树
- JavaScript面向对象和高级03
- 54. Spiral Matrix