[源码下载]
乐在其中设计模式(C#) - 状态模式(State Pattern)
作者:webabcd
介绍
允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它所属的类。
示例
有一个Message实体类,对它的操作有Insert()和Get()方法,持久化数据在SqlServer数据库中或Xml文件里。在对象内部可以根据用户的不同选择不同的数据持久化方案。
MessageModel
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.State
{
/// <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.State
{
/// <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; }
}
}
}
AbstractMessageState
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.State
{
/// <summary>
/// 状态模式抽象类
/// </summary>
public abstract class AbstractMessageState
{
/// <summary>
/// Context类型
/// </summary>
protected Message _message;
/// <summary>
/// Context类型属性
/// </summary>
public Message Message
{
get { return _message; }
set { _message = value; }
}
/// <summary>
/// 改变状态
/// </summary>
public abstract void StateChangeCheck();
/// <summary>
/// 获取Message
/// </summary>
/// <returns></returns>
public abstract List<MessageModel> Get();
/// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public abstract bool Insert(MessageModel mm);
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.State
{
/// <summary>
/// 状态模式抽象类
/// </summary>
public abstract class AbstractMessageState
{
/// <summary>
/// Context类型
/// </summary>
protected Message _message;
/// <summary>
/// Context类型属性
/// </summary>
public Message Message
{
get { return _message; }
set { _message = value; }
}
/// <summary>
/// 改变状态
/// </summary>
public abstract void StateChangeCheck();
/// <summary>
/// 获取Message
/// </summary>
/// <returns></returns>
public abstract List<MessageModel> Get();
/// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public abstract bool Insert(MessageModel mm);
}
}
XmlMessage
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.State
{
/// <summary>
/// Xml方式操作Message
/// </summary>
public class XmlMessage : AbstractMessageState
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="message">Context类型参数</param>
public XmlMessage(Message message)
{
this._message = message;
}
/// <summary>
/// 改变状态
/// </summary>
public override void StateChangeCheck()
{
// 如果UserId是admin则变换状态为SqlMessage
if (this._message.UserId == "admin")
{
this._message.AbstractMessageState = new SqlMessage(this._message);
}
}
/// <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;
}
/// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public override bool Insert(MessageModel mm)
{
// 代码略
return true;
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.State
{
/// <summary>
/// Xml方式操作Message
/// </summary>
public class XmlMessage : AbstractMessageState
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="message">Context类型参数</param>
public XmlMessage(Message message)
{
this._message = message;
}
/// <summary>
/// 改变状态
/// </summary>
public override void StateChangeCheck()
{
// 如果UserId是admin则变换状态为SqlMessage
if (this._message.UserId == "admin")
{
this._message.AbstractMessageState = new SqlMessage(this._message);
}
}
/// <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;
}
/// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public override bool Insert(MessageModel mm)
{
// 代码略
return true;
}
}
}
SqlMessage
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.State
{
/// <summary>
/// Sql方式操作Message
/// </summary>
public class SqlMessage : AbstractMessageState
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="message">Context类型参数</param>
public SqlMessage(Message message)
{
this._message = message;
}
/// <summary>
/// 改变状态
/// </summary>
public override void StateChangeCheck()
{
}
/// <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;
}
/// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public override bool Insert(MessageModel mm)
{
// 代码略
return true;
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.State
{
/// <summary>
/// Sql方式操作Message
/// </summary>
public class SqlMessage : AbstractMessageState
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="message">Context类型参数</param>
public SqlMessage(Message message)
{
this._message = message;
}
/// <summary>
/// 改变状态
/// </summary>
public override void StateChangeCheck()
{
}
/// <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;
}
/// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public override bool Insert(MessageModel mm)
{
// 代码略
return true;
}
}
}
Message
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.State
{
/// <summary>
/// Context类
/// </summary>
public class Message
{
/// <summary>
/// 类型为状态模式抽象类的私有变量
/// </summary>
private AbstractMessageState _abstractMessageState;
/// <summary>
/// 类型为状态模式抽象类的属性
/// </summary>
public AbstractMessageState AbstractMessageState
{
get { return _abstractMessageState; }
set { _abstractMessageState = value; }
}
/// <summary>
/// UserId私有变量
/// </summary>
private string _userId;
/// <summary>
/// UserId
/// </summary>
public string UserId
{
get { return _userId; }
set { _userId = value; }
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="userId">UserId</param>
public Message(string userId)
{
this._userId = userId;
_abstractMessageState = new XmlMessage(this);
}
/// <summary>
/// 获取Message
/// </summary>
/// <returns></returns>
public List<MessageModel> Get()
{
// 修改状态
_abstractMessageState.StateChangeCheck();
return _abstractMessageState.Get();
}
/// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public bool Insert(MessageModel mm)
{
// 修改状态
_abstractMessageState.StateChangeCheck();
return _abstractMessageState.Insert(mm);
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.State
{
/// <summary>
/// Context类
/// </summary>
public class Message
{
/// <summary>
/// 类型为状态模式抽象类的私有变量
/// </summary>
private AbstractMessageState _abstractMessageState;
/// <summary>
/// 类型为状态模式抽象类的属性
/// </summary>
public AbstractMessageState AbstractMessageState
{
get { return _abstractMessageState; }
set { _abstractMessageState = value; }
}
/// <summary>
/// UserId私有变量
/// </summary>
private string _userId;
/// <summary>
/// UserId
/// </summary>
public string UserId
{
get { return _userId; }
set { _userId = value; }
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="userId">UserId</param>
public Message(string userId)
{
this._userId = userId;
_abstractMessageState = new XmlMessage(this);
}
/// <summary>
/// 获取Message
/// </summary>
/// <returns></returns>
public List<MessageModel> Get()
{
// 修改状态
_abstractMessageState.StateChangeCheck();
return _abstractMessageState.Get();
}
/// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public bool Insert(MessageModel mm)
{
// 修改状态
_abstractMessageState.StateChangeCheck();
return _abstractMessageState.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.State;
public partial class State : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Message m = new Message("admin");
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("user");
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 />");
}
}
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.State;
public partial class State : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Message m = new Message("admin");
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("user");
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
SQL方式获取Message 2007-3-6 22:58:17
True
XML方式获取Message 2007-3-6 22:58:17
参考
http://www.dofactory.com/Patterns/PatternState.aspx
OK
[源码下载]
True
SQL方式获取Message 2007-3-6 22:58:17
True
XML方式获取Message 2007-3-6 22:58:17
参考
http://www.dofactory.com/Patterns/PatternState.aspx
OK
[源码下载]
本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344585,如需转载请自行联系原作者