名称 Template Method
结构  设计模式——模板模式
意图 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。Te m p l a t e M e t h o d 使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。
适用性
  • 一次性实现一个算法的不变的部分,并将可变的行为留给子类来实现。
  • 各子类中公共的行为应被提取出来并集中到一个公共父类中以避免代码重复。这是O p d y k e 和J o h n s o n 所描述过的“重分解以一般化”的一个很好的例子[ O J 9 3 ]。首先识别现有代码中的不同之处,并且将不同之处分离为新的操作。最后,用一个调用这些新的操作的模板方法来替换这些不同的代码。
  • 控制子类扩展。模板方法只在特定点调用“h o o k ”操作(参见效果一节),这样就只允许在这些点进行扩展。
Code Example
 1设计模式——模板模式// Template Method
 2设计模式——模板模式
 3设计模式——模板模式// Intent: "Define the skeleton of an algorithm in an operation, deferring 
 4设计模式——模板模式// some steps to subclasses. Template Method lets subclasses redefine 
 5设计模式——模板模式// certain steps of an algorithm without changing the algorithm's structure." 
 6设计模式——模板模式
 7设计模式——模板模式// For further information, read "Design Patterns", p325, Gamma et al.,
 8设计模式——模板模式// Addison-Wesley, ISBN:0-201-63361-2
 9设计模式——模板模式
10

相关文章:

  • 2021-12-17
  • 2022-01-02
  • 2021-06-23
  • 2021-08-21
猜你喜欢
  • 2021-08-10
  • 2022-12-23
  • 2022-01-25
  • 2021-12-17
  • 2022-01-03
  • 2021-11-30
相关资源
相似解决方案