装饰器模式

装饰器设计模式

定义

装饰器模式,在不改变现有对象结构的情况下,动态地给该对象增加一些职责(即增加其额外功能)的模式,它属于对象结构型模式。

装饰器用来包装原有的类,在对使用者透明的情况下做功能的增强,比如java中的BufferedInputStream可以对其包装的InputStream做增强,从而提供缓冲功能。

使用场景

MyBatis里的缓存设计,就是装饰器模式的典型应用。

首先,MyBatis执行器是MyBatis调度的核心,它负责SQL语句的生成和执行。

在创建SqlSession的时候,会创建这个执行器,默认的执行器是SimpleExecutor

但是为了给执行器增加缓存的职责,就变成了在SimpleExecutor上一层添加了CachingExecutor

CachingExecutor中的实际操作还是委托给SimpleExecutor去执行,只是在执行前后增加了缓存的操作。

首先,来看看它的装饰过程。

复制代码
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
    //默认的执行器
    executorType = executorType == null ? defaultExecutorType : executorType;
    executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
    Executor executor;
    if (ExecutorType.BATCH == executorType) {
        executor = new BatchExecutor(this, transaction);
    } else if (ExecutorType.REUSE == executorType) {
        executor = new ReuseExecutor(this, transaction);
    } else {
        executor = new SimpleExecutor(this, transaction);
    }
    //使用缓存执行器来装饰
    if (cacheEnabled) {
        executor = new CachingExecutor(executor);
    }
    executor = (Executor) interceptorChain.pluginAll(executor);
    return executor;
}

当SqlSession执行方法的时候,则会先调用到CachingExecutor,来看查询方法。

复制代码
public class CachingExecutor implements Executor {
    @Override
    public <E> List<E> query()throws SQLException {
        Cache cache = ms.getCache();
        if (cache != null) {
            flushCacheIfRequired(ms);
            if (ms.isUseCache() && resultHandler == null) {
                List<E> list = (List<E>) tcm.getObject(cache, key);
                if (list == null) {
                    list = delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
                    tcm.putObject(cache, key, list); // issue #578 and #116
                }
                return list;
            }
        }
        return delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
    }
}

这里的代码,如果开启了缓存,则先从缓存中获取结果。如果没有开启缓存或者缓存中没有结果,则再调用SimpleExecutor执行器去数据库中查询。

希望对原有类的功能做增强,但又不希望增加过多子类时,可以使用装饰器模式来达到同样的效果。

实践经验

总结

使用装饰器模式做了功能的增强,对使用者来说只需要做简单的组合就能继续使用原功能。

相关推荐
佩奇的技术笔记4 天前
从Java的JDK源码中学设计模式之装饰器模式
java·设计模式·装饰器模式
何中应6 天前
【设计模式-3.5】结构型——装饰器模式
java·设计模式·装饰器模式
秋名RG13 天前
深入理解Java装饰器模式:动态扩展对象功能的优雅之道
java·python·装饰器模式
常某某的好奇心1 个月前
装饰模式(Decorator Pattern)
装饰器模式
lczdyx1 个月前
从Flask到智能体:装饰器模式在AI系统中的架构迁移实践
人工智能·python·语言模型·架构·flask·装饰器模式
星星点点洲1 个月前
【设计模式区别】装饰器模式和适配器模式区别
设计模式·适配器模式·装饰器模式
程序员JerrySUN2 个月前
设计模式每日硬核训练 Day 12:装饰器模式(Decorator Pattern)完整讲解与实战应用
设计模式·装饰器模式
aiden:)2 个月前
星巴克咖啡下单系统:UML 类图解析与代码实现
设计模式·软件工程·uml·装饰器模式
〆、风神2 个月前
装饰器模式与模板方法模式实现MyBatis-Plus QueryWrapper 扩展
mybatis·装饰器模式·模板方法模式
qq_529835352 个月前
装饰器模式:如何用Java打扮一个对象?
java·开发语言·装饰器模式