在Java中,-Impl后缀是钩子方法的合法命名约定吗?
我检查了一些旧代码,在模板方法实现中发现了以下命名约定在Java中,-Impl后缀是钩子方法的合法命名约定吗?,java,design-patterns,naming-conventions,template-method-pattern,Java,Design Patterns,Naming Conventions,Template Method Pattern,我检查了一些旧代码,在模板方法实现中发现了以下命名约定 // TEMPLATE METHOD // Check condition and fail fast if condition is met. // Otherwise call the hook method (to be implemented by subclasses). @Override public boolean accept(String text) { if (condition) { retu
// TEMPLATE METHOD
// Check condition and fail fast if condition is met.
// Otherwise call the hook method (to be implemented by subclasses).
@Override
public boolean accept(String text) {
if (condition) {
return false;
}
// call to the hook method. Each subclass implements its own logic
return acceptImpl(text);
}
// HOOK METHOD
protected abstract boolean acceptImpl(String text);
我希望钩子方法名为doAccept()或acceptHook()而不是acceptImpl()
在实践中,钩子方法是否使用“-Impl”后缀?或者这是一种令人困惑的命名实践?首先,请注意,这里的示例不是GoF定义中的钩子方法。在327-328页上,GoF模板方法模式定义了模板方法调用的操作类型,包括以下内容
- 原始操作(即抽象操作)李>
- 钩子操作,提供默认行为,子类可以在必要时扩展