热门问题
时间线
聊天
视角
模板方法模式
来自维基百科,自由的百科全书
Remove ads
模板方法(英语:template method),是一种行为型软件设计模式。这种设计模式是一种控制反转的实现方式。因为高层代码不再确定(控制)算法的处理流程。

模板方法是一个定义在父类别的方法,负责处理流程、算法的不变部分。模板方法会调用多个定义在父类别的其他工具方法,而这些方法是算法的可变部分,有可能只是抽象方法并没有实作,或者是只有空体的钩子方法。模板方法仅决定这些抽象方法的执行顺序,这些抽象方法由子类别负责实作,并且子类别不允许覆盖模板方法(即不能重写处理流程)。
用法
模板方法模式多用在:
- 某些类别的演算法中,实做了相同的方法,造成程式码的重复。
- 控制子类别必须遵守的一些事项。
结构

在上面的UML类图中,AbstractClass
定义一个模板方法templateMethod()
运算,它定义了行为的骨干即模板:
- 实现行为的不变部份。
- 向自身发送消息
primitive1()
和primitive2()
,由于它们实现于SubClass1
,这允许了子类提供这个算法的这些部份的可变实现。
用例
下面是C++的例子:

#include <iostream>
#include <memory>
class View { // AbstractClass
public:
// defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm.
virtual void doDisplay() {}
// implements a template method defining the skeleton of an algorithm.
// The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects.
void display() {
setFocus();
doDisplay();
resetFocus();
}
virtual ~View() = default;
private:
void setFocus() {
std::cout << "View::setFocus\n";
}
void resetFocus() {
std::cout << "View::resetFocus\n";
}
};
class MyView : public View { // ConcreteClass
// implements the primitive operations to carry out subclass-specific steps of the algorithm.
void doDisplay() override {
// render the view's contents
std::cout << "MyView::doDisplay\n";
}
};
int main() {
// The smart pointers prevent memory leaks
std::unique_ptr<View> myview = std::make_unique<MyView>();
myview->display();
}
这个程序的输出为:
View::setFocus
MyView::doDisplay
View::resetFocus
Remove ads
下面是Python的例子:
from abc import ABC, abstractmethod
class View(ABC):
@abstractmethod
def do_display(self): pass
def display(self):
self.__set_focus()
self.do_display()
self.__reset_focus()
def __set_focus(self):
print("View: set focus")
def __reset_focus(self):
print("View: reset focus")
class MyView(View):
def do_display(self):
print("View: do display")
其执行:
>>> view = MyView()
>>> view.display()
View: set focus
View: do display
View: reset focus
Remove ads
下面是Java的例子:
/**
* An abstract class that is common to several games in
* which players play against the others, but only one is
* playing at a given time.
*/
abstract class Game {
private int playersCount;
abstract void initializeGame();
abstract void makePlay(int player);
abstract boolean endOfGame();
abstract void printWinner();
/* A template method : */
final void playOneGame(int playersCount) {
this.playersCount = playersCount;
initializeGame();
int j = 0;
while (!endOfGame()){
makePlay(j);
j = (j + 1) % playersCount;
}
printWinner();
}
}
//Now we can extend this class in order to implement actual games:
class Chess extends Game {
/* Implementation of necessary concrete methods */
void initializeGame() {
// ...
}
void makePlay(int player) {
// ...
}
boolean endOfGame() {
// ...
}
void printWinner() {
// ...
}
/* Specific declarations for the chess game. */
// ...
}
public class Player {
public static void main(String[] args) {
Game chessGame = new Chess();
chessGame.initializeGame();
chessGame.playOneGame(1); //call template method
}
}
Remove ads
引用
Wikiwand - on
Seamless Wikipedia browsing. On steroids.
Remove ads