如何通过装饰器模式提升设计模式学习笔记(十)的实践应用?

2026-05-19 17:541阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1091个文字,预计阅读时间需要5分钟。

如何通过装饰器模式提升设计模式学习笔记(十)的实践应用?

%E8%A3%85%E9%A5%B0%E5%99%A8%E6%A8%A1%E5%BC%8F%EF%BC%9A%E6%8C%87%E4%B8%8D%E4%BF%AE%E6%94%B9%E7%8E%B0%E6%9C%89%E5%AF%B9%E8%B1%A1%E7%BB%93%E6%9E%84%E7%9A%84%E6%83%85%E5%86%B5%E4%B8%8B%EF%BC%8C%E5%8A%A8%E6%80%81%E5%9C%B0%E7%BB%99%E8%AF%A5%E5%AF%B9%E8%B1%A1%E5%A2%9E%E5%8A%A0%E9%A2%9D%E5%A4%96%E5%8A%9F%E8%83%BD%E3%80%82%E4%B8%80%E3%80%81%E8%A3%85%E9%A5%B0%E5%99%A8%E6%A8%A1%E5%BC%8F%E4%BB%8B%E7%BB%8D%EF%BC%8C%E8%A3%85%E9%A5%B0%E5%99%A8%E6%A8%A1%E5%BC%8F%E5%85%81%E8%AE%B8%E5%90%91%E4%B8%80%E4%B8%AA%E7%8E%B0%E6%9C%89%E7%9A%84%E5%AF%B9%E8%B1%A1%E6%B7%BB%E5%8A%A0%E6%96%B0%E7%9A%84%E5%8A%9F%E8%83%BD%EF%BC%8C%E5%90%8C%E6%97%B6%E4%B8%8D%E6%94%B9%E5%8F%98%E5%85%B6%E7%BB%93%E6%9E%9C%E3%80%82

装饰器(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(防止多线程并发访问)的众多附加功能的缓存类。都是装饰器的应用:

参考资料

mp.weixin.qq.com/s/hDJs6iG_YPww7yeiPxmZLw?

c.biancheng.net/view/1366.html

如何通过装饰器模式提升设计模式学习笔记(十)的实践应用?

本文共计1091个文字,预计阅读时间需要5分钟。

如何通过装饰器模式提升设计模式学习笔记(十)的实践应用?

%E8%A3%85%E9%A5%B0%E5%99%A8%E6%A8%A1%E5%BC%8F%EF%BC%9A%E6%8C%87%E4%B8%8D%E4%BF%AE%E6%94%B9%E7%8E%B0%E6%9C%89%E5%AF%B9%E8%B1%A1%E7%BB%93%E6%9E%84%E7%9A%84%E6%83%85%E5%86%B5%E4%B8%8B%EF%BC%8C%E5%8A%A8%E6%80%81%E5%9C%B0%E7%BB%99%E8%AF%A5%E5%AF%B9%E8%B1%A1%E5%A2%9E%E5%8A%A0%E9%A2%9D%E5%A4%96%E5%8A%9F%E8%83%BD%E3%80%82%E4%B8%80%E3%80%81%E8%A3%85%E9%A5%B0%E5%99%A8%E6%A8%A1%E5%BC%8F%E4%BB%8B%E7%BB%8D%EF%BC%8C%E8%A3%85%E9%A5%B0%E5%99%A8%E6%A8%A1%E5%BC%8F%E5%85%81%E8%AE%B8%E5%90%91%E4%B8%80%E4%B8%AA%E7%8E%B0%E6%9C%89%E7%9A%84%E5%AF%B9%E8%B1%A1%E6%B7%BB%E5%8A%A0%E6%96%B0%E7%9A%84%E5%8A%9F%E8%83%BD%EF%BC%8C%E5%90%8C%E6%97%B6%E4%B8%8D%E6%94%B9%E5%8F%98%E5%85%B6%E7%BB%93%E6%9E%9C%E3%80%82

装饰器(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(防止多线程并发访问)的众多附加功能的缓存类。都是装饰器的应用:

参考资料

mp.weixin.qq.com/s/hDJs6iG_YPww7yeiPxmZLw?

c.biancheng.net/view/1366.html

如何通过装饰器模式提升设计模式学习笔记(十)的实践应用?