第15章 模版方法模式(Template Method)

摘要:Template Method模式是比较简单的设计模式之一,但它却是代码复用的一项基本的技术,在类库中尤其重要。

主要内容

1.概述

2.Template Method解说

3..NET中的Template Method模式

4.适用性及实现要点

概述

变化一直以来都是软件设计的永恒话题,在XP编程中提倡拥抱变化,积极应对。如何更好的去抓住变化点,应对变化?如何更好的提高代码复用?通过学习Template Method模式,您应该有一个新的认识。

意图

定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。Template Method使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。[-GOF《设计模式》]

结构图

图1 Template Method 模式结构图

生活中的例子

模板方法定义了一个操作中算法的骨架,而将一些步骤延迟到子类中。房屋建筑师在开发新项目时会使用模板方法。一个典型的规划包括一些建筑平面图,每个平面图体现了不同部分。在一个平面图中,地基、结构、上下水和走线对于每个房间都是一样的。只有在建筑的后期才开始有差别而产生了不同的房屋样式。

图2 使用建筑图为例子的Template Method模式

Template Method模式解说

李建忠老师说过一句话,如果你只想掌握一种设计模式的话,那这个模式一定是Template Method模式。对于这个问题,我想可能是仁者见仁,智者见智,但是有一点不能否认的Template Method模式是非常简单而且几乎是无处不用,很少有人没有用过它。下面我们以一个简单的数据库查询的例子来说明Template Method模式(注意:这个例子在实际数据库开发中并没有任何实际意义,这里仅仅是为了作为示例而已)。

假如我们需要简单的读取Northwind数据库中的表的记录并显示出来。对于数据库操作,我们知道不管读取的是哪张表,它一般都应该经过如下这样的几步:

1.连接数据库(Connect)

2.执行查询命令(Select)

3.显示数据(Display)

4.断开数据库连接(Disconnect)

这些步骤是固定的,但是对于每一张具体的数据表所执行的查询却是不一样的。显然这需要一个抽象角色,给出顶级行为的实现。如下图:

图3

Template Method模式的实现方法是从上到下,我们首先给出顶级框架DataAccessObject的实现逻辑:

publicabstractclassDataAccessObject

{
protectedstringconnectionString;

protectedDataSetdataSet;

publicvirtualvoidConnect()

{
connectionString=

"Server=Rj-097;UserId=sa;Password=sa;Database=Northwind";

}

publicabstractvoidSelect();

publicabstractvoidDisplay();


publicvirtualvoidDisconnect()

{
connectionString="";
}

//The"TemplateMethod"

publicvoidRun()

{
Connect();

Select();

Display();

Disconnect();
}
}

显然在这个顶级的框架DataAccessObject中给出了固定的轮廓,方法Run()便是模版方法,Template Method模式也由此而得名。而对于Select()和Display()这两个抽象方法则留给具体的子类去实现,如下图:

图4

示意性实现代码:

classCategories:DataAccessObject

{
publicoverridevoidSelect()
{
stringsql="selectCategoryNamefromCategories";

SqlDataAdapterdataAdapter=newSqlDataAdapter(

sql,connectionString);

dataSet=newDataSet();

dataAdapter.Fill(dataSet,"Categories");

}

publicoverridevoidDisplay()

{

Console.WriteLine("Categories----");

DataTabledataTable=dataSet.Tables["Categories"];

foreach(DataRowrowindataTable.Rows)

{

Console.WriteLine(row["CategoryName"].ToString());

}

Console.WriteLine();

}
}

classProducts:DataAccessObject

{
publicoverridevoidSelect()

{
stringsql="selecttop10ProductNamefromProducts";

SqlDataAdapterdataAdapter=newSqlDataAdapter(

sql,connectionString);

dataSet=newDataSet();

dataAdapter.Fill(dataSet,"Products");

}

publicoverridevoidDisplay()

{

Console.WriteLine("Products----");

DataTabledataTable=dataSet.Tables["Products"];

foreach(DataRowrowindataTable.Rows)

{
Console.WriteLine(row["ProductName"].ToString());

}

Console.WriteLine();

}

}

再来看看客户端程序的调用,不需要再去调用每一个步骤的方法:

publicclassApp

{
staticvoidMain()
{

DataAccessObjectdao;


dao=newCategories();

dao.Run();


dao=newProducts();

dao.Run();

//Waitforuser

Console.Read();

}

}

在上面的例子中,需要注意的是:

1.对于Connect()和Disconnect()方法实现为了virtual,而Select()和Display()方法则为abstract,这是因为如果这个方法有默认的实现,则实现为virtual,否则为abstract。

2.Run()方法作为一个模版方法,它的一个重要特征是:在基类里定义,而且不能够被派生类更改。有时候它是私有方法(private method),但实际上它经常被声明为protected。它通过调用其它的基类方法(覆写过的)来工作,但它经常是作为初始化过程的一部分被调用的,这样就没必要让客户端程序员能够直接调用它了。

3.在一开始我们提到了不管读的是哪张数据表,它们都有共同的操作步骤,即共同点。因此可以说Template Method模式的一个特征就是剥离共同点。

.NET 中的Template Method模式

.NET Framework中Template Method模式的使用可以说是无处不在,比如说我们需要自定义一个文本控件,会让它继承于RichTextBox,并重写其中部分事件,如下例所示:

publicclassMyRichTextBox:RichTextBox

{

privatestaticboolm_bPaint=true;

privatestringm_strLine="";

privateintm_nContentLength=0;

privateintm_nLineLength=0;

privateintm_nLineStart=0;

privateintm_nLineEnd=0;

privatestringm_strKeywords="";

privateintm_nCurSelection=0;


protectedoverridevoidOnSelectionChanged(EventArgse)

{
m_nContentLength=this.TextLength;

intnCurrentSelectionStart=SelectionStart;

intnCurrentSelectionLength=SelectionLength;

m_bPaint=false;

m_nLineStart=nCurrentSelectionStart;

while((m_nLineStart>0)&&(Text[m_nLineStart-1]!=',')&&(Text[m_nLineStart-1]!='{')&&(Text[m_nLineStart-1]!='('))

m_nLineStart--;

m_nLineEnd=nCurrentSelectionStart;

while((m_nLineEnd<Text.Length)&&(Text[m_nLineEnd]!=',')&&(Text[m_nLineEnd]!='}')&&(Text[m_nLineEnd]!=')')&&(Text[m_nLineEnd]!='{'))

m_nLineEnd++;


m_nLineLength=m_nLineEnd-m_nLineStart;

m_strLine=Text.Substring(m_nLineStart,m_nLineLength);

this.SelectionStart=m_nLineStart;

this.SelectionLength=m_nLineLength;


m_bPaint=true;

}

protectedoverridevoidOnTextChanged(EventArgse)

{
//重写OnTextChanged
}
}

其中OnSelectionChanged()和OnTextChanged()便是Template Method模式中的基本方法之一,也就是子步骤方法,它们的调用已经在RichTextBox中实现了。

实现要点

1.Template Method模式是一种非常基础性的设计模式,在面向对象系统中有着大量的应用。它用最简洁的机制(虚函数的多态性)为很多应用程序框架提供了灵活的扩展点,是代码复用方面的基本实现结构。

2.除了可以灵活应对子步骤的变化外,“不用调用我,让我来调用你”的反向控制结构是Template Method的典型应用。

3.在具体实现方面,被Template Method调用的虚方法可以具有实现,也可以没有任何实现(抽象方法,纯虚方法),但一般推荐将它们设置为protected方法。[李建忠]

适用性

1.一次性实现一个算法的不变的部分,并将可变的行为留给子类来实现。

2.各子类中公共的行为应被提取出来并集中到一个公共父类中以避免代码重复。这是Opdyke和Johnson所描述过的“重分解以一般化”的一个很好的例子。首先识别现有代码中的不同之处,并且将不同之处分离为新的操作。最后,用一个调用这些新的操作的模板方法来替换这些不同的代码。

3.控制子类扩展。模板方法只在特定点调用“Hook”操作,这样就只允许在这些点进行扩展。

总结

Template Method模式是非常简单的一种设计模式,但它却是代码复用的一项基本的技术,在类库中尤其重要。

本篇文章写的比较简单,请大家见谅。更多的设计模式文章可以访问《.NET设计模式系列文章

参考资料

Erich Gamma等,《设计模式:可复用面向对象软件的基础》,机械工业出版社

Robert C.Martin,《敏捷软件开发:原则、模式与实践》,清华大学出版社

阎宏,《Java与模式》,电子工业出版社

Alan Shalloway James R. Trott,《Design Patterns Explained》,中国电力出版社

MSDN WebCast 《C#面向对象设计模式纵横谈(14):Template Method模版方法模式(结构型模式)》

作者: TerryLee
出处: http://terrylee.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
If you have ever bought any programming books, you might have noticed that there are two types of them: books that are too short to understand the topic and books that are too long making it inevitable that you get bored. We've tried hard to avoid both of these categories with Design Patterns Explained Simply. This book is fast and simple way to get the idea behind each of the 29 popular design patterns. The book is not tied to any specific programming language and will be good for you as long as you have a basic knowledge of OOP. Most chapters are available on the website, so you can check out the simplicity of the language in the book and the way materials are presented. Why should I read this book? It's simple. It's written in clear and simple language that makes it easy to read and understand. It's short. Yes, there are no useless demos or huge code listings — just clear and easy-to-understand descriptions with many graphical examples. When you finish reading this book, you'll have good reason to go to your boss and ask him for apromotion. Why? Because using design patterns will allow you to get your tasks done twice as fast, to write better code and to create efficient and reliable software architecture. How do I become a programming ninja? The main difference between a ninja and a novice is the knowledge of secret coding tricks, as well as the awareness of most pitfalls and the ability to avoid them. Design patterns were created as a bible for avoiding problems related to software design. Doesn’t that make it a true ninja’s handbook? Table of Contents Creational patterns Abstract Factory Builder Factory Method Object Pool Prototype Singleton Structural patterns Adapter Bridge Composite Decorator Facade Flyweight Private Class Data Proxy Behavioral patterns Chain of Responsibility Command Interpreter Iterator Mediator Memento Null Object Observer State Strategy Template Method Visitor

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值