简单方法模式
一、概述
简单工厂模式是一个由工厂对象通过静态方法或者实例方法创建对象,封装对象的创建过程(基于条件不同创建不同的产品)。
二、流程分析

三、角色分析
1、具体工厂(Configuration)
2、抽象产品 (Executor)
3、具体产品 (SimpleExecutor、CachingExecutor、BatchExecutor)
四、实例分析
mybatis中执行器创建过程源码如下:
Configuration中的Executor创建过程,基于ExecutorType不同创建不同的执行器
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; }
五、优缺点
优点:解耦(对象应用者与对象创建之间的解耦),实现起来简单
缺点:可扩展性差(创建产品工厂方法不够灵活、新增产品时需要在工厂方法里面扩展)

浙公网安备 33010602011771号