程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 設計模式(C#) - 模板方法模式(Template Method Pattern)

設計模式(C#) - 模板方法模式(Template Method Pattern)

編輯:關於C#

返回“設計模式(C#)系列文章索引”

介紹

定義一個操作中的算法的骨架,而將一些步驟延遲到子類中。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; }
    }
  }
}

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;
      }
    }
  }
}

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;
    }
  }
}

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;
    }
  }
}

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 />");
  }
}

運行結果

無權獲取 2007-3-13 22:29:55

SQL方式獲取Message 2007-3-13 22:29:55

參考

http://www.dofactory.com/Patterns/PatternTemplate.aspx

OK

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved