概述
The Template pattern defines a structure for sub classes in which steps of an algorithm and their order are defined. This ensures that the sub classes follow the exact same steps, providing better overview and consistency. It also allows to define default implementations for steps that can be overridden by subclasses.
模板方法模式就是给定一个算法的步骤,但是具体算法实现交给子类完成。还是比较好理解的。
本文类图
Template本文类图代码示例
BaseSteps的templateMethod就是模板方法,他定义了具体的算法步骤。
BaseSteps的templateMethod就是模板方法ConcreA是BaseSteps中算法的具体实现。
ConcreAConcreB是BaseSteps中算法的具体实现。
ConcreB测试结果
TemplateMethod的测试结果参考资料
网友评论