乐在其中设计模式(C#) - 状态模式(State Pattern).doc
-
资源ID:69685085
资源大小:398KB
全文页数:9页
- 资源格式: DOC
下载积分:15金币
快捷下载
会员登录下载
微信登录下载
三方登录下载:
微信扫一扫登录
友情提示
2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
|
乐在其中设计模式(C#) - 状态模式(State Pattern).doc
介绍允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它所属的类。示例有一个Message实体类,对它的操作有Insert()和Get()方法,持久化数据在SqlServer数据库中或Xml文件里。在对象内部可以根据用户的不同选择不同的数据持久化方案。MessageModelusing 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; AbstractMessageStateusing 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); XmlMessageusing 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; SqlMessageusing 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; Messageusing 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); clientusing 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 />"); 运行结果TrueSQL方式获取Message 2007-3-6 22:58:17TrueXML方式获取Message 2007-3-6 22:58:17