说明:
模板方法模式(Template Method Pattern)是一种行为型设计模式,它定义了一个算法的框架,并允许子类实现其中的具体步骤。在模板方法模式中,算法的整体结构在父类中定义,但具体的步骤实现延迟到子类中。
模板方法模式的核心思想是将算法的结构和具体实现分离,通过定义一个模板方法作为算法的框架,将算法中可变的部分延迟到子类中实现,从而使得算法的框架稳定,而具体实现可以灵活变化。
下面是模板方法模式的主要角色:
-
抽象类(Abstract Class):抽象类定义了算法的框架,其中包含一个或多个抽象方法和模板方法。抽象方法用于延迟到子类实现的具体步骤,而模板方法定义了算法的框架,其中包含一系列的步骤,这些步骤可以是具体方法或抽象方法。
-
具体类(Concrete Class):具体类是抽象类的子类,它实现了抽象方法,完成算法的具体步骤。
模板方法模式的应用场景包括:
-
当多个类有相似的算法结构,但具体步骤不同的情况下,可以使用模板方法模式来封装公共的算法结构,避免代码重复。
-
当希望通过子类来扩展和改变算法的具体实现时,可以使用模板方法模式。通过继承抽象类,并重写其中的抽象方法,可以实现算法的个性化定制。
-
当需要控制算法的执行流程,并在不同的步骤中进行特定的操作时,可以使用模板方法模式。
翻译:
The Template Method Pattern is a behavioral design pattern that defines the framework of an algorithm and allows subclasses to implement the specific steps within that algorithm. In the Template Method Pattern, the overall structure of the algorithm is defined in the parent class, but the concrete step implementations are deferred to the subclasses.
The core idea of the Template Method Pattern is to separate the structure of the algorithm from its specific implementation by defining a template method as the algorithm's framework, with the variable parts of the algorithm delegated to be implemented by subclasses, thus keeping the framework stable while allowing flexible variations in the specific implementation.
The main roles in the Template Method Pattern are:
-
Abstract Class: The abstract class defines the framework of the algorithm, which includes one or more abstract methods and template methods. The abstract methods are the steps that are deferred to be implemented by subclasses, while the template method defines the framework of the algorithm, consisting of a series of steps that can be concrete methods or abstract methods.
-
Concrete Class: The concrete class is a subclass of the abstract class and implements the abstract methods to complete the specific steps of the algorithm.
The Template Method Pattern is commonly used in the following scenarios:
-
When multiple classes have similar algorithm structures but different specific steps, the Template Method Pattern can be used to encapsulate the common algorithm structure and avoid code duplication.
-
When it is desired to extend and change the specific implementation of an algorithm through subclasses, the Template Method Pattern can be used. By inheriting the abstract class and overriding its abstract methods, personalized customization of the algorithm can be achieved.
-
When there is a need to control the execution flow of an algorithm and perform specific operations at different steps, the Template Method Pattern can be used.
下面以一个制作饮料的例子来说明模板方法模式的应用:
abstract class Beverage {
// 模板方法,定义了制作饮料的框架
public final void makeBeverage() {
boilWater();
brew();
pourInCup();
if (customerWantsCondiments()) {
addCondiments();
}
}
// 具体步骤的实现由子类完成
protected abstract void brew();
protected abstract void addCondiments();
// 具体步骤的实现在父类中定义
private void boilWater() {
System.out.println("Boiling water");
}
private void pourInCup() {
System.out.println("Pouring into cup");
}
// 钩子方法,子类可以覆盖该方法来控制算法的某个步骤
protected boolean customerWantsCondiments() {
return true;
}
}
class Coffee extends Beverage {
protected void brew() {
System.out.println("Brewing coffee");
}
protected void addCondiments() {
System.out.println("Adding milk and sugar");
}
// 覆盖钩子方法,控制是否添加调料
protected boolean customerWantsCondiments() {
return false;
}
}
class Tea extends Beverage {
protected void brew() {
System.out.println("Steeping tea");
}
protected void addCondiments() {
System.out.println("Adding lemon");
}
}
public class TemplateMethodPatternExample {
public static void main(String[] args) {
Beverage coffee = new Coffee();
coffee.makeBeverage();
System.out.println("-------------------");
Beverage tea = new Tea();
tea.makeBeverage();
}
}
这个例子展示了模板方法模式的应用。抽象类 Beverage
定义了制作饮料的模板方法 makeBeverage()
,并且每个具体的饮料类(如咖啡和茶)提供了自己的实现。通过调用模板方法,我们可以实现不同类型饮料的制作过程,而具体的步骤实现则留给了子类来完成。这样,算法的框架在抽象类中定义,而具体的步骤则由子类决定,实现了算法的灵活性和可扩展性。