Spring中的模板方法模式

Spring中有很多地方用到了模板方法模式,本文主要介绍AbstractBeanDefinitionParser中用到的模板方法模式。

具体的模板方法模式的详细介绍可以参见:https://www.cnblogs.com/zhanglei93/p/6021086.html

 

AbstractBeanDefinitionParser 中的public final BeanDefinition parse(Element element, ParserContext parserContext) 方法概括了解析bean的算法框架,而该方法调用的parseInternal(element, parserContext)方法则由子类实现,是典型的模板方法模式。AbstractSingleBeanDefinitionParser作为AbstractBeanDefinitionParser 的子类实现了parseInternal方法。AbstractSingleBeanDefinitionParser是抽象类, 该类也使用了模板方法模式。该类中parseInternal方法调用了该类中定义的doParse方法,而doParse方法是一个空方法, 其中并未做任何操作。继承AbstractSingleBeanDefinitionParser的子类可以重写doParse方法。比如要想实现一个自定义的标签,可以继承AbstractSingleBeanDefinitionParser类,重写doParse方法,在重写的doParse方法中实现自定义标签的解析。

 

 

原文地址:https://www.cnblogs.com/chwy/p/13906750.html