这篇文章告诉你装饰器模式及其在Java IO类和MyBatis中的应用

装饰器(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 装饰器模式结构

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

image-20220331203832443

  • 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中有很多装饰器的应用:

IO

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

2.2 MyBatis 中 Cache的应用

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

image-20220331224248502

参考资料

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

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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

归思君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值