装饰器(Decorator)模式:指不改变现有对象结构的情况下,动态地给该对象增加额外功能。
装饰器模式允许向一个现有的对象添加新的功能,同时不改变其结果。比如Java 中的IO框架中,FileInputStream
(处理文件)、ByteArrayInputStream
(处理字节数组)、BufferedInputStream
(带缓存的处理类)等就是对InputStream
进行的功能扩展,这就是装饰器模式的典型应用。比如下面就是以缓存方式读取输入流:
InputStream inputStream = new BufferedInputStream(new FileInputStream("test.txt")); byte[] data = new byte[128]; while(inputStream.read(data) != -1){ //... }
装饰器主要使用组合关系来创建一个装饰对象,用于包裹真实对象,并在保持真实对象的类结构不变的前提下为其提供额外的功能。具体的基本结构如下所示:
Component
:抽象构件,定义一个抽象接口以规范准备接收附加责任的对象ComponentA
:具体构件,实现抽象构件,通过装饰角色为其添加一些职责Decorator
:抽象装饰构件,并包含具体构件的实例DecoratorA、DecoratorB
:实现抽象装饰构件的具体装饰构件,包含实现抽象装饰的相关方法Client
:客户端根据上面的类图可以实现如下代码:
/** * @description: 抽象构件角色 * @author: wjw * @date: 2022/3/31 */ public interface Component { public void operation(); } /** * @description:具体构件角色 * @author: wjw * @date: 2022/3/31 */ public class ComponentA implements Component{ public ComponentA() { System.out.println("创建具体构件componentA"); } @Override public void operation() { System.out.println("我是具体构件A的operation方法"); } } /** * @description: 抽象装饰 * @author: wjw * @date: 2022/3/31 */ public class Decorator implements Component{ private Component component; public Decorator(Component component) { this.component = component; } @Override public void operation() { component.operation(); } } /** * @description: 具体装饰角色A * @author: wjw * @date: 2022/3/31 */ public class DecoratorA extends Decorator{ public DecoratorA(Component component) { super(component); } @Override public void operation() { super.operation(); addedFunction(); } /** * 增加的额外功能 */ public void addedFunction() { System.out.println("我是为具体装饰角色A增加额外功能方法addedFunction"); } } /** * @description: 具体装饰角色B * @author: wjw * @date: 2022/3/31 */ public class DecoratorB extends Decorator{ public DecoratorB(Component component) { super(component); } @Override public void operation() { super.operation(); addedFunction(); } private void addedFunction() { System.out.println("为具体装饰角色增加额外的功能B"); } } /** * @description: 客户端 * @author: wjw * @date: 2022/3/31 */ public class DecoratiorClient { public static void main(String[] args) { Component componentA = new ComponentA(); componentA.operation(); Decorator decoratorA = new DecoratorA(componentA); decoratorA.operation(); } }
在开始介绍中提到,IO中有很多装饰器的应用:
如上图所示,比如InputStream
后面的若干装饰器类都是对其的功能扩展。
Cache 中除了有数据存储和缓存的基本功能外还有其他附加的 Cache 类,比如有 FifoCache(先进先出)、LruCache(最近最少使用LRU)、SychronizedCache(防止多线程并发访问)的众多附加功能的缓存类。都是装饰器的应用: