策略模式(Strategy Pattern)
介绍定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换。本模式使得算法的变化可独立于使用它的客户。示例有一个Message实体类,对它的操作有Insert()和Get()方法,持久化数据在SqlServer数据库中或Xml文件里(两种可互换的算法)。由客户端决定使用哪种算法。MessageModel
using System;using System.Collections.Generic;using System.Text;namespace Pattern.Strategy{ ////// Message实体类 /// public class MessageModel { ////// 构造函数 /// /// Message内容 /// Message发布时间 public MessageModel(string msg, DateTime pt) { this._message = msg; this._publishTime = pt; } private string _message; ////// Message内容 /// public string Message { get { return _message; } set { _message = value; } } private DateTime _publishTime; ////// Message发布时间 /// public DateTime PublishTime { get { return _publishTime; } set { _publishTime = value; } } }}
IMessageStrategy
using System;using System.Collections.Generic;using System.Text;namespace Pattern.Strategy{ ////// 策略接口 /// public interface IMessageStrategy { ////// 获取Message /// ///List Get(); /// /// 插入Message /// /// Message实体对象 ///bool Insert(MessageModel mm); }}
SqlMessage
using System;using System.Collections.Generic;using System.Text;namespace Pattern.Strategy{ ////// Sql方式操作Message /// public class SqlMessage : IMessageStrategy { ////// 获取Message /// ///public List Get() { List l = new List (); l.Add(new MessageModel("SQL方式获取Message", DateTime.Now)); return l; } /// /// 插入Message /// /// Message实体对象 ///public bool Insert(MessageModel mm) { // 代码略 return true; } }}
XmlMessage
using System;using System.Collections.Generic;using System.Text;namespace Pattern.Strategy{ ////// Xml方式操作Message /// public class XmlMessage : IMessageStrategy { ////// 获取Message /// ///public List Get() { List l = new List (); l.Add(new MessageModel("XML方式获取Message", DateTime.Now)); return l; } /// /// 插入Message /// /// Message实体对象 ///public bool Insert(MessageModel mm) { // 代码略 return true; } }}
Message
using System;using System.Collections.Generic;using System.Text;namespace Pattern.Strategy{ ////// Context类 /// public class Message { ////// 声明一个IMessageStrategy类型 /// private IMessageStrategy _strategy; ////// 构造函数 /// /// 具体算法 public Message(IMessageStrategy strategy) { this._strategy = strategy; } ////// 获取Message /// ///public List Get() { return _strategy.Get(); } /// /// 插入Message /// /// Message实体对象 ///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(""); Response.Write(m.Get()[0].Message + " " + m.Get()[0].PublishTime.ToString()); Response.Write(""); m = new Message(new SqlMessage()); Response.Write(m.Insert(new MessageModel("插入", DateTime.Now))); Response.Write(""); Response.Write(m.Get()[0].Message + " " + m.Get()[0].PublishTime.ToString()); Response.Write(""); }}
运行结果
True
XML方式获取Message 2007-2-10 22:42:44 True SQL方式获取Message 2007-2-10 22:42:44