Template Method模式在基类中定义算法的骨架,让子类填充具体步骤而无需改变整体结构。它捕获通用流程,同时允许自定义各个步骤。
Template Method模式的作用
TEMPLATE METHOD → define an algorithm's overall STRUCTURE in a base method, with some STEPS
left for subclasses to implement:
→ the base class controls the overall flow (the "template")
→ subclasses override specific STEPS (the parts that vary)
→ the algorithm's structure is FIXED; the steps are customizable
→ "common process, customizable steps"
示例
{
() {
data = .();
result = .(data);
.(result);
}
() { ; }
() { ; }
() { ; }
}
{ () {...} () {...} () {...} }
