装饰器(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){ //... }
1.1 装饰器模式结构
装饰器主要使用组合关系来创建一个装饰对象,用于包裹真实对象,并在保持真实对象的类结构不变的前提下为其提供额外的功能。具体的基本结构如下所示:
Component
:抽象构件,定义一个抽象接口以规范准备接收附加责任的对象ComponentA
:具体构件,实现抽象构件,通过装饰角色为其添加一些职责Decorator
:抽象装饰构件,并包含具体构件的实例DecoratorA、DecoratorB
:实现抽象装饰构件的具体装饰构件,包含实现抽象装饰的相关方法Client
:客户端
1.2 装饰器模式实现
根据上面的类图可以实现如下代码:
/** * @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(); } }
二、装饰器模式应用场景
2.1 Java IO 类中的应用
在开始介绍中提到,IO中有很多装饰器的应用:
如上图所示,比如InputStream
后面的若干装饰器类都是对其的功能扩展。
2.2 MyBatis 中 Cache的应用
Cache 中除了有数据存储和缓存的基本功能外还有其他附加的 Cache 类,比如有 FifoCache(先进先出)、LruCache(最近最少使用LRU)、SychronizedCache(防止多线程并发访问)的众多附加功能的缓存类。都是装饰器的应用:
参考资料
热门文章
- 2月8日|V2ray/SSR/Clash/Shadowrocket每天更新18.4M/S免费节点订阅链接地址分享
- 1月23日|Shadowrocket/V2ray/Clash/SSR每天更新19.9M/S免费节点订阅链接地址分享
- 成都宠物领养中心官网收容中心电话(成都宠物领养吧贴吧)
- 中国宠物食品排名(中国宠物食品排名榜)
- 宠物产品品牌大全(宠物产品品牌大全图片)
- vue的路由组件的引入以及路由组件懒加载和router-link
- 1月25日|Shadowrocket/Clash/SSR/V2ray每天更新21.7M/S免费节点订阅链接地址分享
- 宠物粮食生产厂家有哪些品牌 宠物粮食生产厂家有哪些品牌的
- 1月13日|SSR/Shadowrocket/Clash/V2ray每天更新21.7M/S免费节点订阅链接地址分享
- 深圳宠物领养中心有哪些(深圳市领养宠物中心)