Template Method
英文简要描述
Intent
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses.
Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
How to
AbstractClass
defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm.
implements a template method defining the skeleton of an algorithm. The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects.
ConcreteClass
implements the primitive operations to carry out subclass-specific steps of the algorithm.
Known cases
A lot
UML
模板方法通常和工厂方法一起使用,以下是样例:
上例的uml
primitive operation
抽象方法,子类必须实现
hook operation
钩子方法,有默认实现,子类可以重写其行为