TEMPLATE METHOD(模板方法)----- 类行为型模式

[size=large]1、意图
定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。TemplateMethod使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。
2、动机
考虑一个提供Application和Document类的应用框架。Application类负责打开一个已有的已外部形式存储的文档,如一个文件。一旦一个文档中的信息从该文件中读出后,它就由一个Document对象表示。
用框架构建的应用可以通过继承Application和Document来满足特定的需求。例如,一个绘图应用定义DrawApplication和DrawDocument子类;一个电子表格应用定义SpreadsheetApplication和SpreadsheetDocument子类,如下图所示。
抽象的的Application类在它的OpenDocument操作中定义了打开和读取一个文档的算法:[/size]
  void Application::OpenDocument(const char* name){
if(!canOpenDocument(name)){
//cannot handle this document
return;
}
Document* doc = DoCreateDocument();
if(doc){
_doc->AddDocument(doc);
aboutToOpenDocument(doc);
doc->Open();
doc->DoRead();
}
}

[size=large] OpenDocument定义了打开一个文档的每一个步骤。它检查该文档是否能被打开,创建与应用相关的Document对象,将它加到文档集合中,并且从一个文件中读取该Document。
我们称OpenDocument为一个模板方法(template method)。一个模板方法用一些抽象的操作定义一个算法,而子类将重定义这些操作以提供具体的行为。Application的子类将定义检查一个文档是否能被打开(CanOpenDocument)和创建文档(DoCreateDocument)的具体算法步骤。Document子类将定义读取文档(DoRead)的算法步骤。如果需要,模板方法也可定义一个操作(AboutToOpenDocument)让Application子类知道该文档合适被打开。
通过使用抽象操作定义一个算法中的一些步骤,模板方法确定了它们的先后顺序,但它允许Application和Document子类改变这些步骤以满足它们各自的需求。
3、适用性
模板方法应用于下列的情况:
一次性实现一个算法的不变得部分,并将可变的行为留给子类来实现。
各子类中公共的行为应被提取出来并集中到一个公共父类中以避免代码重复。首先识别现有代码中的不同之处,并将不同之处分离为新的操作。最后,用一个调用这些新的操作的模板方法来替换这些不同的代码。
控制子类扩展。模板方法中只有特定点调用“hook”操作,这样就只允许在这些点进行扩展。
4、结构[/size]

[img]http://dl.iteye.com/upload/attachment/0082/7271/eb12c7bf-d943-3738-bbe9-0de07d0eee61.jpg[/img]

[size=large]5、代码示例[/size]

/**
* 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 {

protected int playersCount;
abstract void initializeGame();
abstract void makePlay(int player);
abstract boolean endOfGame();
abstract void printWinner();

/* A template method : */
public 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 Monopoly extends Game {

/* Implementation of necessary concrete methods */
void initializeGame() {
// Initialize players
// Initialize money
}
void makePlay(int player) {
// Process one turn of player
}
boolean endOfGame() {
// Return true if game is over
// according to Monopoly rules
}
void printWinner() {
// Display who won
}
/* Specific declarations for the Monopoly game. */

// ...
}

class Chess extends Game {

/* Implementation of necessary concrete methods */
void initializeGame() {
// Initialize players
// Put the pieces on the board
}
void makePlay(int player) {
// Process a turn for the player
}
boolean endOfGame() {
// Return true if in Checkmate or
// Stalemate has been reached
}
void printWinner() {
// Display the winning player
}
/* Specific declarations for the chess game. */

// ...
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值