[索引页]
[源码下载]
作者: webabcd
介绍
定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。Template Method使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。
示例
有一个Message实体类,对它的操作有Get()方法,每次Get()之前要使用ValidateUser()方法来检查当前用户是否有权限,获取数据可能在SqlServer数据库中或Xml文件里。(这里先检查用户的权限,再获取数据就是算法的骨架,获取数据延迟到子类中实现)
MessageModel
[源码下载]
乐在其中设计模式(C#) - 模板方法模式(Template Method Pattern)
作者: webabcd
介绍
定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。Template Method使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。
示例
有一个Message实体类,对它的操作有Get()方法,每次Get()之前要使用ValidateUser()方法来检查当前用户是否有权限,获取数据可能在SqlServer数据库中或Xml文件里。(这里先检查用户的权限,再获取数据就是算法的骨架,获取数据延迟到子类中实现)
MessageModel
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.TemplateMethod
{
/// <summary>
/// Message实体类
/// </summary>
public class MessageModel
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="msg">Message内容</param>
/// <param name="pt">Message发布时间</param>
public MessageModel( string msg, DateTime pt)
{
this._message = msg;
this._publishTime = pt;
}
private string _message;
/// <summary>
/// Message内容
/// </summary>
public string Message
{
get { return _message; }
set { _message = value; }
}
private DateTime _publishTime;
/// <summary>
/// Message发布时间
/// </summary>
public DateTime PublishTime
{
get { return _publishTime; }
set { _publishTime = value; }
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.TemplateMethod
{
/// <summary>
/// Message实体类
/// </summary>
public class MessageModel
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="msg">Message内容</param>
/// <param name="pt">Message发布时间</param>
public MessageModel( string msg, DateTime pt)
{
this._message = msg;
this._publishTime = pt;
}
private string _message;
/// <summary>
/// Message内容
/// </summary>
public string Message
{
get { return _message; }
set { _message = value; }
}
private DateTime _publishTime;
/// <summary>
/// Message发布时间
/// </summary>
public DateTime PublishTime
{
get { return _publishTime; }
set { _publishTime = value; }
}
}
}
AbstractMessageTemplate
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.TemplateMethod
{
/// <summary>
/// 模板方法抽象类
/// </summary>
public abstract class AbstractMessageTemplate
{
/// <summary>
/// UserId
/// </summary>
protected string _userId;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="userId">UserId</param>
public AbstractMessageTemplate( string userId)
{
this._userId = userId;
}
/// <summary>
/// 验证用户是否合法
/// </summary>
/// <returns></returns>
public virtual bool ValidateUser()
{
if ( this._userId == "admin")
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 获取MessageModel列表
/// </summary>
/// <returns></returns>
public abstract List<MessageModel> Get();
/// <summary>
/// 模板方法:先验证用户是否合法,再获取MessageModel列表
/// </summary>
/// <returns></returns>
public List<MessageModel> TemplateMethodGet()
{
if (ValidateUser())
{
return Get();
}
else
{
List<MessageModel> l = new List<MessageModel>();
l.Add( new MessageModel( "无权获取", DateTime.Now));
return l;
}
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.TemplateMethod
{
/// <summary>
/// 模板方法抽象类
/// </summary>
public abstract class AbstractMessageTemplate
{
/// <summary>
/// UserId
/// </summary>
protected string _userId;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="userId">UserId</param>
public AbstractMessageTemplate( string userId)
{
this._userId = userId;
}
/// <summary>
/// 验证用户是否合法
/// </summary>
/// <returns></returns>
public virtual bool ValidateUser()
{
if ( this._userId == "admin")
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 获取MessageModel列表
/// </summary>
/// <returns></returns>
public abstract List<MessageModel> Get();
/// <summary>
/// 模板方法:先验证用户是否合法,再获取MessageModel列表
/// </summary>
/// <returns></returns>
public List<MessageModel> TemplateMethodGet()
{
if (ValidateUser())
{
return Get();
}
else
{
List<MessageModel> l = new List<MessageModel>();
l.Add( new MessageModel( "无权获取", DateTime.Now));
return l;
}
}
}
}
SqlMessage
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.TemplateMethod
{
/// <summary>
/// Sql方式操作Message
/// </summary>
public class SqlMessage : AbstractMessageTemplate
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="userId">UserId</param>
public SqlMessage( string userId)
: base(userId)
{
}
/// <summary>
/// 获取Message
/// </summary>
/// <returns></returns>
public override List<MessageModel> Get()
{
List<MessageModel> l = new List<MessageModel>();
l.Add( new MessageModel( "SQL方式获取Message", DateTime.Now));
return l;
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.TemplateMethod
{
/// <summary>
/// Sql方式操作Message
/// </summary>
public class SqlMessage : AbstractMessageTemplate
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="userId">UserId</param>
public SqlMessage( string userId)
: base(userId)
{
}
/// <summary>
/// 获取Message
/// </summary>
/// <returns></returns>
public override List<MessageModel> Get()
{
List<MessageModel> l = new List<MessageModel>();
l.Add( new MessageModel( "SQL方式获取Message", DateTime.Now));
return l;
}
}
}
XmlMessage
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.TemplateMethod
{
/// <summary>
/// Xml方式操作Message
/// </summary>
public class XmlMessage : AbstractMessageTemplate
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="userId">UserId</param>
public XmlMessage( string userId)
: base(userId)
{
}
/// <summary>
/// 获取Message
/// </summary>
/// <returns></returns>
public override List<MessageModel> Get()
{
List<MessageModel> l = new List<MessageModel>();
l.Add( new MessageModel( "XML方式获取Message", DateTime.Now));
return l;
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.TemplateMethod
{
/// <summary>
/// Xml方式操作Message
/// </summary>
public class XmlMessage : AbstractMessageTemplate
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="userId">UserId</param>
public XmlMessage( string userId)
: base(userId)
{
}
/// <summary>
/// 获取Message
/// </summary>
/// <returns></returns>
public override List<MessageModel> Get()
{
List<MessageModel> l = new List<MessageModel>();
l.Add( new MessageModel( "XML方式获取Message", DateTime.Now));
return l;
}
}
}
client
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Pattern.TemplateMethod;
public partial class TemplateMethod : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
AbstractMessageTemplate m = new XmlMessage( "user");
Response.Write(m.TemplateMethodGet()[0].Message + " " + m.TemplateMethodGet()[0].PublishTime.ToString());
Response.Write( "<br />");
m = new SqlMessage( "admin");
Response.Write(m.TemplateMethodGet()[0].Message + " " + m.TemplateMethodGet()[0].PublishTime.ToString());
Response.Write( "<br />");
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Pattern.TemplateMethod;
public partial class TemplateMethod : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
AbstractMessageTemplate m = new XmlMessage( "user");
Response.Write(m.TemplateMethodGet()[0].Message + " " + m.TemplateMethodGet()[0].PublishTime.ToString());
Response.Write( "<br />");
m = new SqlMessage( "admin");
Response.Write(m.TemplateMethodGet()[0].Message + " " + m.TemplateMethodGet()[0].PublishTime.ToString());
Response.Write( "<br />");
}
}
运行结果
无权获取 2007-3-13 22:29:55
SQL方式获取Message 2007-3-13 22:29:55
参考
http://www.dofactory.com/Patterns/PatternTemplate.aspx
OK
[源码下载]
无权获取 2007-3-13 22:29:55
SQL方式获取Message 2007-3-13 22:29:55
参考
http://www.dofactory.com/Patterns/PatternTemplate.aspx
OK
[源码下载]
本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344588,如需转载请自行联系原作者