11.2 、模板方法模式中的方法
基本方法: 抽象方法(Abstract Method), 具体方法(Concrete Method), 钩子方法(Hook mehtod)
钩子方法:一个由抽象类声明并实现,而子类加以扩展。通常是抽象类给出的空实现,作为默认实现,子类加以扩展。
代码:
abstract public class AbstractClass
{ /**钩子方法*/
public void hookMethod()
{}
/**抽象方法*/
public abstract void abstractMethod();
public void concreteMethod() /**具体方法*/
{
System.out.println("This is a concrete method.");
}
}
public class ConcreteClass extends AbstractClass
{
public void hookMethod()/**扩展钩子方法*/
{
System.out.println("This is a re-implemented hook method.");
}
public void abstractMethod()/**实现抽象方法*/
{
System.out.println("This is an implementation of an abstract method.");
}
}