前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >设计模式学习笔记(十)装饰器模式及其应用

设计模式学习笔记(十)装饰器模式及其应用

作者头像
归思君
发布2023-10-16 09:05:51
2230
发布2023-10-16 09:05:51
举报
文章被收录于专栏:归思君的技术博客

装饰器(Decorator)模式:指不改变现有对象结构的情况下,动态地给该对象增加额外功能。

一、装饰器模式介绍

装饰器模式允许向一个现有的对象添加新的功能,同时不改变其结果。比如Java 中的IO框架中,FileInputStream(处理文件)、ByteArrayInputStream(处理字节数组)、BufferedInputStream(带缓存的处理类)等就是对InputStream进行的功能扩展,这就是装饰器模式的典型应用。比如下面就是以缓存方式读取输入流:

代码语言:javascript
复制
InputStream inputStream = new BufferedInputStream(new FileInputStream("test.txt"));
byte[] data = new byte[128];
while(inputStream.read(data) != -1){
    //...
} 

1.1 装饰器模式结构

装饰器主要使用组合关系来创建一个装饰对象,用于包裹真实对象,并在保持真实对象的类结构不变的前提下为其提供额外的功能。具体的基本结构如下所示:

image-20220331203832443
image-20220331203832443
  • Component:抽象构件,定义一个抽象接口以规范准备接收附加责任的对象
  • ComponentA:具体构件,实现抽象构件,通过装饰角色为其添加一些职责
  • Decorator:抽象装饰构件,并包含具体构件的实例
  • DecoratorA、DecoratorB:实现抽象装饰构件的具体装饰构件,包含实现抽象装饰的相关方法
  • Client:客户端

1.2 装饰器模式实现

根据上面的类图可以实现如下代码:

代码语言:javascript
复制
/**
 * @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中有很多装饰器的应用:

IO
IO

如上图所示,比如InputStream后面的若干装饰器类都是对其的功能扩展。

2.2 MyBatis 中 Cache的应用

Cache 中除了有数据存储和缓存的基本功能外还有其他附加的 Cache 类,比如有 FifoCache(先进先出)、LruCache(最近最少使用LRU)、SychronizedCache(防止多线程并发访问)的众多附加功能的缓存类。都是装饰器的应用:

image-20220331224248502
image-20220331224248502

参考资料

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

http://c.biancheng.net/view/1366.html

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-03-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、装饰器模式介绍
    • 1.1 装饰器模式结构
      • 1.2 装饰器模式实现
      • 二、装饰器模式应用场景
        • 2.1 Java IO 类中的应用
          • 2.2 MyBatis 中 Cache的应用
          • 参考资料
          相关产品与服务
          数据保险箱
          数据保险箱(Cloud Data Coffer Service,CDCS)为您提供更高安全系数的企业核心数据存储服务。您可以通过自定义过期天数的方法删除数据,避免误删带来的损害,还可以将数据跨地域存储,防止一些不可抗因素导致的数据丢失。数据保险箱支持通过控制台、API 等多样化方式快速简单接入,实现海量数据的存储管理。您可以使用数据保险箱对文件数据进行上传、下载,最终实现数据的安全存储和提取。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档