钩子(hook)是之前开发当中经常提及的一个词,wiki 上的一个定义:HookMethod 是定义在模版类中一个方法的调用HookMethod,而此方法又可以被 模版类子类覆盖,子类可以决定具体的操作 .
- public class Algorithm {
- public void templateMethod() {
- :
- .
- hookMethod();
- .
- :
- }
- public void hookMethod() {
- // default implementation
- }
- }
- public class RefinedAlgorithm extends Algorithm {
- public void hookMethod() {
- // refined implementation
- }
- }