乐在个中设计格局(C#) – 状态格局(State Pattern)

[索引页] [源码下载]

乐在其中设计形式(C#) – 状态形式(State Pattern)

作者:webabcd

介绍
允许2个对象在其内部景观改变时改变它的一举一动。对象看起来仿佛修改了它所属的类。

示例
有2个Message实体类,对它的操作有Insert()和Get()方法,持久化数据在SqlServer数据库中或Xml文件里。在对象内部能够依据用户的不比取舍分裂的数目持久化方案。
C# 1

MessageModel

C# 2using System;
C# 3using System.Collections.Generic;
C# 4using System.Text;
C# 5
C# 6namespace Pattern.State
C# 7C# 8C# 9{
C# 10C# 11    /**//// <summary>
C# 12    /// Message实体类
C# 13    /// </summary>
C# 14    public class MessageModel
C# 15C# 16    C# 17{
C# 18C# 19        /**//// <summary>
C# 20        /// 构造函数
C# 21        /// </summary>
C# 22        /// <param name=”msg”>Message内容</param>
C# 23        /// <param name=”pt”>Message宣布时间</param>
C# 24        public MessageModel(string msg, DateTime pt)
C# 25C# 26        C# 27{
C# 28            this._message = msg;
C# 29            this._publishTime = pt;
C# 30        }
C# 31
C# 32        private string _message;
C# 33C# 34        /**//// <summary>
C# 35        /// Message内容
C# 36        /// </summary>
C# 37        public string Message
C# 38C# 39        C# 40{
C# 41C# 42            get C# 43{ return _message; }
C# 44C# 45            set C# 46{ _message = value; }
C# 47        }
C# 48
C# 49        private DateTime _publishTime;
C# 50C# 51        /**//// <summary>
C# 52        /// Message公布时间
C# 53        /// </summary>
C# 54        public DateTime PublishTime
C# 55C# 56        C# 57{
C# 58C# 59            get C# 60{ return _publishTime; }
C# 61C# 62            set C# 63{ _publishTime = value; }
C# 64        }
C# 65    }
C# 66}
C# 67

AbstractMessageState

C# 68using System;
C# 69using System.Collections.Generic;
C# 70using System.Text;
C# 71
C# 72namespace Pattern.State
C# 73C# 74C# 75{
C# 76C# 77    /**//// <summary>
C# 78    /// 状态情势抽象类
C# 79    /// </summary>
C# 80    public abstract class AbstractMessageState
C# 81C# 82    C# 83{
C# 84C# 85        /**//// <summary>
C# 86        /// Context类型
C# 87        /// </summary>
C# 88        protected Message _message;
C# 89
C# 90C# 91        /**//// <summary>
C# 92        /// Context类型属性
C# 93        /// </summary>
C# 94        public Message Message
C# 95C# 96        C# 97{
C# 98C# 99            get C# 100{ return _message; }
C# 101C# 102            set C# 103{ _message = value; }
C# 104        }
C# 105
C# 106C# 107        /**//// <summary>
C# 108        /// 改变状态
C# 109        /// </summary>
C# 110        public abstract void StateChangeCheck();
C# 111
C# 112C# 113        /**//// <summary>
C# 114        /// 获取Message
C# 115        /// </summary>
C# 116        /// <returns></returns>
C# 117        public abstract List<MessageModel> Get();
C# 118
C# 119C# 120        /**//// <summary>
C# 121        /// 插入Message
C# 122        /// </summary>
C# 123        /// <param name=”mm”>Message实体对象</param>
C# 124        /// <returns></returns>
C# 125        public abstract bool Insert(MessageModel mm);
C# 126    }
C# 127}
C# 128

XmlMessage

C# 129using System;
C# 130using System.Collections.Generic;
C# 131using System.Text;
C# 132
C# 133namespace Pattern.State
C# 134C# 135C# 136{
C# 137C# 138    /**//// <summary>
C# 139    /// Xml方式操作Message
C# 140    /// </summary>
C# 141    public class XmlMessage : AbstractMessageState
C# 142C# 143    C# 144{
C# 145C# 146        /**//// <summary>
C# 147        /// 构造函数
C# 148        /// </summary>
C# 149        /// <param name=”message”>Context类型参数</param>
C# 150        public XmlMessage(Message message)
C# 151C# 152        C# 153{
C# 154            this._message = message;
C# 155        }
C# 156
C# 157C# 158        /**//// <summary>
C# 159        /// 改变状态
C# 160        /// </summary>
C# 161        public override void StateChangeCheck()
C# 162C# 163        C# 164{
C# 165            // 即便UserId是admin则转移状态为SqlMessage
C# 166            if (this._message.UserId == “admin”)
C# 167C# 168            C# 169{
C# 170                this._message.AbstractMessageState = new SqlMessage(this._message);
C# 171            }
C# 172        }
C# 173
C# 174C# 175        /**//// <summary>
C# 176        /// 获取Message
C# 177        /// </summary>
C# 178        /// <returns></returns>
C# 179        public override List<MessageModel> Get()
C# 180C# 181        C# 182{
C# 183            List<MessageModel> l = new List<MessageModel>();
C# 184            l.Add(new MessageModel(“XML格局得到Message”, Date提姆e.Now));
C# 185
C# 186            return l;
C# 187        }
C# 188
C# 189C# 190        /**//// <summary>
C# 191        /// 插入Message
C# 192        /// </summary>
C# 193        /// <param name=”mm”>Message实体对象</param>
C# 194        /// <returns></returns>
C# 195        public override bool Insert(MessageModel mm)
C# 196C# 197        C# 198{
C# 199            // 代码略
C# 200            return true;
C# 201        }
C# 202    }
C# 203}
C# 204

SqlMessage

C# 205using System;
C# 206using System.Collections.Generic;
C# 207using System.Text;
C# 208
C# 209namespace Pattern.State
C# 210C# 211C# 212{
C# 213C# 214    /**//// <summary>
C# 215    /// Sql情势操作Message
C# 216    /// </summary>
C# 217    public class SqlMessage : AbstractMessageState
C# 218C# 219    C# 220{
C# 221C# 222        /**//// <summary>
C# 223        /// 构造函数
C# 224        /// </summary>
C# 225        /// <param name=”message”>Context类型参数</param>
C# 226        public SqlMessage(Message message)
C# 227C# 228        C# 229{
C# 230            this._message = message;
C# 231        }
C# 232
C# 233C# 234        /**//// <summary>
C# 235        /// 改变状态
C# 236        /// </summary>
C# 237C#,        public override void StateChangeCheck()
C# 238C# 239        C# 240{
C# 241
C# 242        }
C# 243
C# 244C# 245        /**//// <summary>
C# 246        /// 获取Message
C# 247        /// </summary>
C# 248        /// <returns></returns>
C# 249        public override List<MessageModel> Get()
C# 250C# 251        C# 252{
C# 253            List<MessageModel> l = new List<MessageModel>();
C# 254            l.Add(new MessageModel(“SQL情势赢得Message”, DateTime.Now));
C# 255
C# 256            return l;
C# 257        }
C# 258
C# 259C# 260        /**//// <summary>
C# 261        /// 插入Message
C# 262        /// </summary>
C# 263        /// <param name=”mm”>Message实体对象</param>
C# 264        /// <returns></returns>
C# 265        public override bool Insert(MessageModel mm)
C# 266C# 267        C# 268{
C# 269            // 代码略
C# 270            return true;
C# 271        }
C# 272    }
C# 273}
C# 274

Message

C# 275using System;
C# 276using System.Collections.Generic;
C# 277using System.Text;
C# 278
C# 279namespace Pattern.State
C# 280C# 281C# 282{
C# 283C# 284    /**//// <summary>
C# 285    /// Context类
C# 286    /// </summary>
C# 287    public class Message
C# 288C# 289    C# 290{
C# 291C# 292        /**//// <summary>
C# 293        /// 类型为状态形式抽象类的村办变量
C# 294        /// </summary>
C# 295        private AbstractMessageState _abstractMessageState;
C# 296
C# 297C# 298        /**//// <summary>
C# 299        /// 类型为状态方式抽象类的天性
C# 300        /// </summary>
C# 301        public AbstractMessageState AbstractMessageState
C# 302C# 303        C# 304{
C# 305C# 306            get C# 307{ return _abstractMessageState; }
C# 308C# 309            set C# 310{ _abstractMessageState = value; }
C# 311        }       
C# 312
C# 313C# 314        /**//// <summary>
C# 315        /// UserId私有变量
C# 316        /// </summary>
C# 317        private string _userId;
C# 318
C# 319C# 320        /**//// <summary>
C# 321        /// UserId
C# 322        /// </summary>
C# 323        public string UserId
C# 324C# 325        C# 326{
C# 327C# 328            get C# 329{ return _userId; }
C# 330C# 331            set C# 332{ _userId = value; }
C# 333        }
C# 334
C# 335C# 336        /**//// <summary>
C# 337        /// 构造函数
C# 338        /// </summary>
C# 339        /// <param name=”userId”>UserId</param>
C# 340        public Message(string userId)
C# 341C# 342        C# 343{
C# 344            this._userId = userId;
C# 345
C# 346            _abstractMessageState = new XmlMessage(this);
C# 347        }
C# 348
C# 349C# 350        /**//// <summary>
C# 351        /// 获取Message
C# 352        /// </summary>
C# 353        /// <returns></returns>
C# 354        public List<MessageModel> Get()
C# 355C# 356        C# 357{
C# 358            // 修改意况
C# 359            _abstractMessageState.StateChangeCheck();
C# 360
C# 361            return _abstractMessageState.Get();
C# 362        }
C# 363
C# 364C# 365        /**//// <summary>
C# 366        /// 插入Message
C# 367        /// </summary>
C# 368        /// <param name=”mm”>Message实体对象</param>
C# 369        /// <returns></returns>
C# 370        public bool Insert(MessageModel mm)
C# 371C# 372        C# 373{
C# 374            // 修改意况
C# 375            _abstractMessageState.StateChangeCheck();
C# 376
C# 377            return _abstractMessageState.Insert(mm);
C# 378        }
C# 379    }
C# 380}
C# 381

client

C# 382using System;
C# 383using System.Data;
C# 384using System.Configuration;
C# 385using System.Collections;
C# 386using System.Web;
C# 387using System.Web.Security;
C# 388using System.Web.UI;
C# 389using System.Web.UI.WebControls;
C# 390using System.Web.UI.WebControls.WebParts;
C# 391using System.Web.UI.HtmlControls;
C# 392
C# 393using Pattern.State;
C# 394
C# 395public partial class State : System.Web.UI.Page
C# 396C# 397C# 398{
C# 399    protected void Page_Load(object sender, EventArgs e)
C# 400C# 401    C# 402{
C# 403        Message m = new Message(“admin”);
C# 404        Response.Write(m.Insert(new MessageModel(“插入”, DateTime.Now)));
C# 405        Response.Write(“<br />”);
C# 406        Response.Write(m.Get()[0].Message + ” ” + m.Get()[0].PublishTime.ToString());
C# 407        Response.Write(“<br />”);
C# 408
C# 409        m = new Message(“user”);
C# 410        Response.Write(m.Insert(new MessageModel(“插入”, DateTime.Now)));
C# 411        Response.Write(“<br />”);
C# 412        Response.Write(m.Get()[0].Message + ” ” + m.Get()[0].PublishTime.ToString());
C# 413        Response.Write(“<br />”);
C# 414    }
C# 415}
C# 416

运营结果
True
SQL格局获取Message 二〇〇五-3-6 22:58:17
True
XML方式获得Message 二零零七-3-6 22:58:17

参考
http://www.dofactory.com/Patterns/PatternState.aspx

OK
[源码下载]

相关文章