基本介绍
模板模式 Template Pattern,在一个抽象类中定义了一个算法骨架,将算法中的某一些步骤做成抽象方法,延迟到子类去实现,子类不能修改主体的算法结构,但能根据具体的需求实现定义某些特定的步骤
UML
角色分析
AbstractClass 抽象模板类 定义算法骨架并且用final 修改子类不能修改 算法骨架中的operation方法可以供子类去具体实现
ConcreteClass 具体实现模板方法的子类
Client 客户端调用者直接使用实现了模板方法的子类
代码实现
现在有一个兑牛奶的需求,步骤都是消毒,加调料,搅拌,打包,根据具体添加的调料不同最终会得到不同风味的牛奶。
// 核心抽象模板类
public abstract class MikeMaker {
/**
* 算法骨架 牛奶的制作过程一定固定子类不能去改变
*/
public final void makeMike() {
disinfect();
addFlavouring();
mixing();
box();
}
public void disinfect() {
System.out.println("牛奶消毒");
}
/**
* 添加备料 抽象模板
*/
public abstract void addFlavouring();
public void mixing() {
System.out.println("搅拌备料");
}
public void box() {
System.out.println("打包调配好的牛奶");
}
}
// 具体的子类实现,这里是纯牛奶可以直接一个空实现
public class PureMike extends MikeMaker{
@Override
public void addFlavouring() {
System.out.println("纯牛奶什么都不加..可以一个空实现");
}
}
public class StrawberryMike extends MikeMaker {
@Override
public void addFlavouring() {
System.out.println("加点草莓");
}
}
public class ChocolateMike extends MikeMaker {
@Override
public void addFlavouring() {
System.out.println("加点巧克力");
}
}
// 客户端调用直接使用子类即可
public class TemplateTest {
public static void main(String[] args) {
new StrawberryMike().makeMike();
new PureMike().makeMike();
new ChocolateMike().makeMike();
}
}
使用细节
模板模式非常实用,在很多框架中都使用到了模板设计模式。比如Spring在IOC容器创建的时候refresh方法里面定义了容器创建的算法骨架过程,而且也暴露比如onfresh()方法的一个空实现让子类去具体实现。
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
通常在需求实现流程固定,但某一些流程又需要特殊处理就可以用到模板方法模式,抽象出一个执行流程骨架,具体的某一些步骤留给子类去实现,这样使得设计更加灵活,但是也会增加类的数量,使得系统更加庞大