C#設計模式--狀態模式

設計模式:

狀態模式(State Pattern)

簡單介紹:

在狀態模式(State Pattern)中,類的行爲是基於它的狀態改變的。這種類型的設計模式屬於行爲型模式。html

在狀態模式中,咱們建立表示各類狀態的對象和一個行爲隨着狀態對象改變而改變的 context 對象。java

舉例子:本例子徹底參考csdn:http://blog.csdn.net/chenssy/article/details/11096391(原做者用java實現)設計模式

酒店房間狀態變換圖:post

狀態模式類圖:

上圖來源於菜鳥教程測試

角色:this

狀態模式包含以下角色: 
       Context: 環境類。能夠包括一些內部狀態。 
       State: 抽象狀態類。State定義了一個全部具體狀態的共同接口,任何狀態都實現這個相同的接口,這樣一來,狀態之間就能夠互相轉換了。 
       StartStates/StopState: 具體狀態類。具體狀態類,用於處理來自Context的請求,每個State的具體類都提供了它對本身請求的實現,因此,當Context改變狀態時行爲也會跟着改變。spa

酒店房間狀態僞類圖:.net

狀態變遷:設計

而後是3個狀態類,這個三個狀態分別對於這:空閒、預訂、入住。其中空閒能夠完成預訂和入住兩個動做,預訂能夠完成入住和退訂兩個動做,入住能夠退房。3d

狀態模式C#代碼舉例

狀態接口

 1     public interface IState
 2     {
 3         /// <summary>
 4         /// 預約房間
 5         /// </summary>
 6         void bookRoom();
 7 
 8         /// <summary>
 9         /// 退訂房間
10         /// </summary>
11         void UnSubscribeRoom();
12 
13         /// <summary>
14         /// 入住
15         /// </summary>
16         void CheckInRoom();
17 
18         /// <summary>
19         /// 退房
20         /// </summary>
21         void CheckOutRoom();
22 
23     }

具體的房間類

  1 /*****************************************************
  2  * ProjectName:  _11DesignPattern_StatePattern
  3  * Description:
  4  * ClassName:    Room
  5  * CLRVersion:   4.0.30319.18444
  6  * Author:       JiYF
  7  * NameSpace:    _11DesignPattern_StatePattern
  8  * MachineName:  JIYONGFEI
  9  * CreateTime:   2017/7/17 17:31:00
 10  * UpdatedTime:  2017/7/17 17:31:00
 11 *****************************************************/
 12 using System;
 13 using System.Collections.Generic;
 14 using System.Linq;
 15 using System.Text;
 16 
 17 namespace _11DesignPattern_StatePattern
 18 {
 19     public class Room
 20     {
 21         /// <summary>
 22         /// 房間的當前狀態
 23         /// </summary>
 24         private IState _state;
 25 
 26         public IState State
 27         {
 28             get { return _state; }
 29             set { _state = value; }
 30         }
 31         /// <summary>
 32         /// 房間的三個狀態
 33         /// </summary>
 34         private IState _freeTimeState; //空閒狀態
 35 
 36         private IState _checkInState;  //入住狀態
 37 
 38         private IState _bookedState;   //預約狀態
 39 
 40         /// <summary>
 41         /// 空閒狀態的set和get
 42         /// </summary>
 43         public IState FreeTimeState
 44         {
 45             get { return _freeTimeState; }
 46             set { _freeTimeState = value; }
 47         }
 48 
 49         /// <summary>
 50         /// 入住狀態的get和set
 51         /// </summary>
 52         public IState CheckInState
 53         {
 54             get { return _checkInState; }
 55             set { _checkInState = value; }
 56         }
 57 
 58         /// <summary>
 59         /// 預約狀態的get和set
 60         /// </summary>
 61         public IState BookedState
 62         {
 63             get { return _bookedState; }
 64             set { _bookedState = value; }
 65         }
 66 
 67 
 68 
 69         public Room()
 70         {
 71             this._freeTimeState = new FreeTimeState(this);
 72             this._checkInState = new CheckInState(this);
 73             this._bookedState = new BookedState(this);
 74             this._state = this._freeTimeState;
 75         }
 76 
 77         /// <summary>
 78         /// 預約房間
 79         /// </summary>
 80         public void bookRoom()
 81         {
 82             this._state.bookRoom();
 83         }
 84 
 85         /// <summary>
 86         /// 退訂房間
 87         /// </summary>
 88         public void UnSubscribeRoom()
 89         {
 90             this._state.UnSubscribeRoom();
 91         }
 92 
 93         /// <summary>
 94         /// 入住
 95         /// </summary>
 96         public void CheckInRoom()
 97         {
 98             this._state.CheckInRoom();
 99         }
100 
101         /// <summary>
102         /// 退房
103         /// </summary>
104         public void CheckOutRoom()
105         {
106             this._state.CheckOutRoom();
107         }
108 
109         public string getRoomState()
110         {
111             return "該房間的狀態是:" + this.State.GetType().ToString();
112         }
113 
114        
115     }
116 }

三個具體的狀態是實現:

FreeTimeState.cs  房間空閒狀態

 1 /*****************************************************
 2  * ProjectName:  _11DesignPattern_StatePattern
 3  * Description:
 4  * ClassName:    FreeTimeState
 5  * CLRVersion:   4.0.30319.18444
 6  * Author:       JiYF
 7  * NameSpace:    _11DesignPattern_StatePattern
 8  * MachineName:  JIYONGFEI
 9  * CreateTime:   2017/7/17 17:34:28
10  * UpdatedTime:  2017/7/17 17:34:28
11 *****************************************************/
12 using System;
13 using System.Collections.Generic;
14 using System.Linq;
15 using System.Text;
16 
17 namespace _11DesignPattern_StatePattern
18 {
19     public class FreeTimeState:IState
20     {
21         private Room _hotelManagement;
22         public FreeTimeState()
23         { }
24 
25         public FreeTimeState(Room hotelManagement)
26         {
27             this._hotelManagement = hotelManagement;
28         }
29         public void bookRoom()
30         {
31             //設置狀態爲已經預約狀態
32             this._hotelManagement.State = this._hotelManagement.BookedState;
33             Console.WriteLine("您已經成功預約了...");
34         }
35 
36         public void UnSubscribeRoom()
37         {
38             //暫不操做
39         }
40 
41         public void CheckInRoom()
42         {
43             this._hotelManagement.State = this._hotelManagement.CheckInState;
44             Console.WriteLine("您已經成功入住...");
45         }
46 
47         public void CheckOutRoom()
48         {
49             //暫不操做
50         }
51     }
52 }

BookedState.cs房間預訂狀態

 1 /*****************************************************
 2  * ProjectName:  _11DesignPattern_StatePattern
 3  * Description:
 4  * ClassName:    BookedState
 5  * CLRVersion:   4.0.30319.18444
 6  * Author:       JiYF
 7  * NameSpace:    _11DesignPattern_StatePattern
 8  * MachineName:  JIYONGFEI
 9  * CreateTime:   2017/7/17 17:34:45
10  * UpdatedTime:  2017/7/17 17:34:45
11 *****************************************************/
12 using System;
13 using System.Collections.Generic;
14 using System.Linq;
15 using System.Text;
16 
17 namespace _11DesignPattern_StatePattern
18 {
19     public class BookedState:IState
20     {
21         private Room _hotelManagement;
22         public BookedState()
23         { }
24 
25         public BookedState(Room hotelManagement)
26         {
27             this._hotelManagement = hotelManagement;
28         }
29         public void bookRoom()
30         {
31             Console.WriteLine("該房間已經預約了...");
32         }
33 
34         public void UnSubscribeRoom()
35         {
36             this._hotelManagement.State = this._hotelManagement.FreeTimeState;
37             Console.WriteLine("退訂成功,歡迎下次光臨...。");
38         }
39 
40         public void CheckInRoom()
41         {
42             this._hotelManagement.State = this._hotelManagement.CheckInState;
43             Console.WriteLine("入住成功...");
44         }
45 
46         public void CheckOutRoom()
47         {
48             //暫不操做
49         }
50     }
51 }

CheckInState.cs房間入住狀態

 1 /*****************************************************
 2  * ProjectName:  _11DesignPattern_StatePattern
 3  * Description:
 4  * ClassName:    CheckInState
 5  * CLRVersion:   4.0.30319.18444
 6  * Author:       JiYF
 7  * NameSpace:    _11DesignPattern_StatePattern
 8  * MachineName:  JIYONGFEI
 9  * CreateTime:   2017/7/17 17:34:36
10  * UpdatedTime:  2017/7/17 17:34:36
11 *****************************************************/
12 using System;
13 using System.Collections.Generic;
14 using System.Linq;
15 using System.Text;
16 
17 namespace _11DesignPattern_StatePattern
18 {
19     public class CheckInState:IState
20     {
21         private Room _hotelManagement;
22         public CheckInState()
23         { }
24 
25         public CheckInState(Room hotelManagement)
26         {
27             this._hotelManagement = hotelManagement;
28         }
29         public void bookRoom()
30         {
31             Console.WriteLine("該房間已經入住...");
32         }
33 
34         public void UnSubscribeRoom()
35         {
36             //暫不操做
37         }
38 
39         public void CheckInRoom()
40         {
41             Console.WriteLine("該房間已經入住...");
42         }
43 
44         public void CheckOutRoom()
45         {
46             this._hotelManagement.State = this._hotelManagement.FreeTimeState;
47             Console.WriteLine("退房成功...");
48         }
49     }
50 }

測試代碼:

 1     class Test
 2     {
 3         static void Main(string[] args)
 4         {
 5             //建立倆間房子
 6             Room[] rooms = new Room[2];
 7             //初始化
 8             for (int i = 0; i < rooms.Length; i++)
 9             {
10                 rooms[i] = new Room();
11             }
12 
13             //第一間房子
14             rooms[0].bookRoom(); //預約
15             rooms[0].CheckInRoom();//入住
16             rooms[0].bookRoom(); //再次預約
17             Console.WriteLine(rooms[0].State);
18             Console.WriteLine("---------漂亮的分割線----------");
19 
20             //第二間房子
21             rooms[1].CheckInRoom(); //入住
22             rooms[1].bookRoom(); //預約
23             rooms[1].CheckInRoom(); //入住
24             rooms[1].bookRoom();//再次預約
25             Console.WriteLine(rooms[1].State);
26             Console.WriteLine("---------漂亮的分割線----------");
27 
28             Console.Read();
29         }
30     }

運行結果:

源代碼工程文件下載

 

0.C#設計模式--簡單工廠模式

1.C#設計模式--工廠方法模式

2.C#設計模式--抽象工廠模式

3.C#設計模式--單例模式

4.C#設計模式--建造者模式

5.C#設計模式--原型模式

6.C#設計模式--設配器模式

7.C#設計模式--裝飾器模式

8.C#設計模式--代理模式

9.C#設計模式--外觀模式

10.C#設計模式--橋接模式

11.C#設計模式--觀察者模式(發佈-訂閱模式)

相關文章
相關標籤/搜索