设计模式:
状态模式(State Pattern)
简单介绍:
在状态模式(State Pattern)中,类的行为是基于它的状态改变的。这种类型的设计模式属于行为型模式。
在状态模式中,我们创建表示各种状态的对象和一个行为随着状态对象改变而改变的 context 对象。
举例子:本例子完全参考csdn:http://blog.csdn.net/chenssy/article/details/11096391(原作者用java实现)
酒店房间状态变换图:
状态模式类图:
上图来源于菜鸟教程
角色:
状态模式包含如下角色:
Context: 环境类。可以包括一些内部状态。
State: 抽象状态类。State定义了一个所有具体状态的共同接口,任何状态都实现这个相同的接口,这样一来,状态之间就可以互相转换了。
StartStates/StopState: 具体状态类。具体状态类,用于处理来自Context的请求,每一个State的具体类都提供了它对自己请求的实现,所以,当Context改变状态时行为也会跟着改变。
酒店房间状态伪类图:
状态变迁:
然后是3个状态类,这个三个状态分别对于这:空闲、预订、入住。其中空闲可以完成预订和入住两个动作,预订可以完成入住和退订两个动作,入住可以退房。
状态模式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 }