钩子(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
- }
- }
本文介绍了软件设计中的模板方法模式及其核心概念——钩子方法。通过示例代码展示了如何在基类中定义一个默认实现的钩子方法,并允许子类进行重写以改变行为。
895

被折叠的 条评论
为什么被折叠?



