程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 設計模式(C#) - 狀態模式(State Pattern)

設計模式(C#) - 狀態模式(State Pattern)

編輯:關於C#

返回“設計模式(C#)系列文章索引”

介紹

允許一個對象在其內部狀態改變時改變它的行為。對象看起來似乎修改了它所屬的類。

示例

有一個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; }
    }
  }
}

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);
  }
}

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;
    }
  }
}

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;
    }
  }
}

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);
    }
  }
}

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 />");
  }
}

運行結果

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved