返回“設計模式(C#)系列文章索引”
介紹
定義一系列的算法,把它們一個個封裝起來,並且使它們可相互替換。本模式使得算法的變化可獨立於使用它的客戶。
示例
有一個Message實體類,對它的操作有Insert()和Get()方法,持久化數據在SqlServer數據庫中或Xml文件裡(兩種可互換的算法)。由客戶端決定使用哪種算法。
MessageModel
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Strategy
{
/**//// <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; }
}
}
}
IMessageStrategy
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Strategy
{
/**//// <summary>
/// 策略接口
/// </summary>
public interface IMessageStrategy
{
/**//// <summary>
/// 獲取Message
/// </summary>
/// <returns></returns>
List<MessageModel> Get();
/**//// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message實體對象</param>
/// <returns></returns>
bool Insert(MessageModel mm);
}
}
SqlMessage
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Strategy
{
/**//// <summary>
/// Sql方式操作Message
/// </summary>
public class SqlMessage : IMessageStrategy
{
/**//// <summary>
/// 獲取Message
/// </summary>
/// <returns></returns>
public List<MessageModel> Get()
{
List<MessageModel> l = new List<MessageModel>();
l.Add(new MessageModel("SQL方式獲取Message", DateTime.Now));
return l;
}
/**//// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message實體對象</param>
/// <returns></returns>
public bool Insert(MessageModel mm)
{
// 代碼略
return true;
}
}
}
XmlMessage
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Strategy
{
/**//// <summary>
/// Xml方式操作Message
/// </summary>
public class XmlMessage : IMessageStrategy
{
/**//// <summary>
/// 獲取Message
/// </summary>
/// <returns></returns>
public List<MessageModel> Get()
{
List<MessageModel> l = new List<MessageModel>();
l.Add(new MessageModel("XML方式獲取Message", DateTime.Now));
return l;
}
/**//// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message實體對象</param>
/// <returns></returns>
public bool Insert(MessageModel mm)
{
// 代碼略
return true;
}
}
}
Message
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Strategy
{
/**//// <summary>
/// Context類
/// </summary>
public class Message
{
/**//// <summary>
/// 聲明一個IMessageStrategy類型
/// </summary>
private IMessageStrategy _strategy;
/**//// <summary>
/// 構造函數
/// </summary>
/// <param name="strategy">具體算法</param>
public Message(IMessageStrategy strategy)
{
this._strategy = strategy;
}
/**//// <summary>
/// 獲取Message
/// </summary>
/// <returns></returns>
public List<MessageModel> Get()
{
return _strategy.Get();
}
/**//// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message實體對象</param>
/// <returns></returns>
public bool Insert(MessageModel mm)
{
return _strategy.Insert(mm);
}
}
}
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.Strategy;
public partial class Strategy : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Message m = new Message(new XmlMessage());
Response.Write(m.Insert(new MessageModel("插入", DateTime.Now)));
Response.Write("<br />");
Response.Write(m.Get()[0].Message + " " + m.Get()[0].PublishTime.ToString());
Response.Write("<br />");
m = new Message(new SqlMessage());
Response.Write(m.Insert(new MessageModel("插入", DateTime.Now)));
Response.Write("<br />");
Response.Write(m.Get()[0].Message + " " + m.Get()[0].PublishTime.ToString());
Response.Write("<br />");
}
}
運行結果
True
XML方式獲取Message 2007-2-10 22:42:44
True
SQL方式獲取Message 2007-2-10 22:42:44
參考
http://www.dofactory.com/Patterns/PatternStrategy.aspx
OK