装饰模式的具体分析可以参考之前的文章,装饰模式. 在学习mybatis源码的时候,再次发现executor包下的代码用了装饰模式。 org.apache.ibatis.session.Configuration下的
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;
}