策略模式(Strategy Pattern)

策略模式(Strategy Pattern)


介绍
定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换。本模式使得算法的变化可独立于使用它的客户。

示例
有一个Message实体类,对它的操作有Insert()和Get()方法,持久化数据在SqlServer数据库中或Xml文件里(两种可互换的算法)。由客户端决定使用哪种算法。
1.jpg.png
MessageModel

http://www.cnblogs.com/Images/OutliningIndicators/None.gifusing System;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Collections.Generic;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Text;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
namespace Pattern.Strategy
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif{
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
/// Message实体类
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
public class MessageModel
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 构造函数
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// <param name="msg">Message内容</param>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <param name="pt">Message发布时间</param>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public MessageModel(string msg, DateTime pt)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
this._message = msg;
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
this._publishTime = pt;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
private string _message;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// Message内容
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public string Message
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
get { return _message; }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
set { _message = value; }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
private DateTime _publishTime;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// Message发布时间
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public DateTime PublishTime
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
get { return _publishTime; }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
set { _publishTime = value; }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cnblogs.com/Images/OutliningIndicators/None.gif


IMessageStrategy

http://www.cnblogs.com/Images/OutliningIndicators/None.gifusing System;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Collections.Generic;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Text;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
namespace Pattern.Strategy
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif{
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
/// 策略接口
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
public interface IMessageStrategy
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 获取Message
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <returns></returns>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        List<MessageModel> Get();
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 插入Message
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// <param name="mm">Message实体对象</param>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <returns></returns>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
bool Insert(MessageModel mm);
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cnblogs.com/Images/OutliningIndicators/None.gif


SqlMessage

http://www.cnblogs.com/Images/OutliningIndicators/None.gifusing System;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Collections.Generic;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Text;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
namespace Pattern.Strategy
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif{
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
/// Sql方式操作Message
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
public class SqlMessage : IMessageStrategy
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 获取Message
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <returns></returns>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public List<MessageModel> Get()
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            List<MessageModel> l = 
new List<MessageModel>();
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            l.Add(
new MessageModel("SQL方式获取Message", DateTime.Now));
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
return l;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 插入Message
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// <param name="mm">Message实体对象</param>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <returns></returns>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public bool Insert(MessageModel mm)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
// 代码略
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
            return true;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cnblogs.com/Images/OutliningIndicators/None.gif


XmlMessage

http://www.cnblogs.com/Images/OutliningIndicators/None.gifusing System;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Collections.Generic;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Text;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
namespace Pattern.Strategy
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif{
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
/// Xml方式操作Message
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
public class XmlMessage : IMessageStrategy
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 获取Message
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <returns></returns>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public List<MessageModel> Get()
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            List<MessageModel> l = 
new List<MessageModel>();
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            l.Add(
new MessageModel("XML方式获取Message", DateTime.Now));
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
return l;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 插入Message
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// <param name="mm">Message实体对象</param>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <returns></returns>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public bool Insert(MessageModel mm)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
// 代码略
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
            return true;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cnblogs.com/Images/OutliningIndicators/None.gif


Message

http://www.cnblogs.com/Images/OutliningIndicators/None.gifusing System;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Collections.Generic;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Text;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
namespace Pattern.Strategy
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif{
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
/// Context
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
public class Message
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 声明一个IMessageStrategy类型
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
private IMessageStrategy _strategy;
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 构造函数
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <param name="strategy">具体算法</param>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public Message(IMessageStrategy strategy)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
this._strategy = strategy;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 获取Message
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <returns></returns>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public List<MessageModel> Get()
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
return _strategy.Get();
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 插入Message
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// <param name="mm">Message实体对象</param>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <returns></returns>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public bool Insert(MessageModel mm)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
return _strategy.Insert(mm);
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cnblogs.com/Images/OutliningIndicators/None.gif



client

http://www.cnblogs.com/Images/OutliningIndicators/None.gifusing System;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Data;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Configuration;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Collections;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Web;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Web.Security;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Web.UI;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Web.UI.WebControls;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Web.UI.WebControls.WebParts;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Web.UI.HtmlControls;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using Pattern.Strategy;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
public partial class Strategy : System.Web.UI.Page
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif{
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Message m = 
new Message(new XmlMessage());
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write(m.Insert(
new MessageModel("插入", DateTime.Now)));
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write("<br />");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write(m.Get()[0].Message + " " + m.Get()[0].PublishTime.ToString());
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write("<br />");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        m = 
new Message(new SqlMessage());
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write(m.Insert(
new MessageModel("插入", DateTime.Now)));
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write("<br />");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write(m.Get()[0].Message + " " + m.Get()[0].PublishTime.ToString());
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write("<br />");
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cnblogs.com/Images/OutliningIndicators/None.gif


运行结果
True
XML方式获取Message 2007-2-10 22:42:44
True
SQL方式获取Message 2007-2-10 22:42:44


果糖网