模板方法
字数: 0

基本概念

Template Method Pattern: 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.
notion image
notion image
  • 泛型?
  • hook → 先占位?
  • JdbcTemplate

相关联系

  • 模板方法模式基于继承机制,它通过扩展子类中的部分内容来改变部分算法。
    • 在类层次上运作, 它是静态的。 
  •  
    策略模式
    基于组合机制,它通过对相应行为提供不同的策略来改变对象的行为。 
    • 在对象层次上运作, 它允许在运行时切换行为。
  • 工厂模式
    模式是模板方法模式的一种特殊形式。 同时, 工厂方法可以作为一个大型模板方法中的一个步骤。

实现代码

抽象类

具体类

测试类

Q & A

What are key challenges associated with a template design pattern?
  • Client code cannot direct the sequence of steps.
  • A subclass can override a method defined in the parent class.
  • More subclass means more scattered codes and difficult maintenance.
2023 - 2026