c# 状态模式

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using System.Timers;
  7 
  8 namespace State
  9 {
 10     abstract class State
 11     {
 12         public abstract void Handle(PlayerState state);
 13         public abstract void Steps(List<int> list);
 14     }
 15 
 16     class Player1 : State 
 17     {
 18         public override void Handle(PlayerState state)
 19         {
 20             if (state.m_Cnum < 2)
 21             {
 22                 state.SetCards(2);
 23             }
 24             state.SetState(new Player2());
 25            // state.Request();
 26         }
 27         public override void Steps(List<int> list)
 28         {
 29             
 30         }
 31     }
 32 
 33     class Player2 : State
 34     {
 35         public override void Handle(PlayerState state)
 36         {
 37             if (state.m_Cnum<3)
 38             {
 39                 state.SetCards(3);
 40             }
 41             state.SetState(new Player3());
 42            // state.Request();
 43         }
 44         public override void Steps(List<int> list)
 45         {
 46 
 47         }
 48     }
 49 
 50     class Player3 : State
 51     {
 52         public override void Handle(PlayerState state)
 53         {
 54             if (state.m_Cnum < 5)
 55             {
 56                 state.SetCards(5);
 57             }
 58             state.SetState(new Player1());
 59             //state.Request();
 60         }
 61         public override void Steps(List<int> list)
 62         {
 63             
 64         }
 65     }
 66 
 67     class PlayerState 
 68     {
 69         private State m_state;
 70         public List<int> m_list = new List<int>();
 71         public int m_Cnum = 0;
 72         //public 
 73         public State GetState() 
 74         {
 75             return m_state;
 76         }
 77 
 78         public void SetState(State state) 
 79         {
 80             m_state = state;
 81             Console.WriteLine("当前状态:" + m_state.GetType().Name);  
 82         }
 83 
 84         public void SetCards(int num) 
 85         {
 86             m_Cnum = num;
 87         }
 88         public void Request() 
 89         {
 90             m_state.Handle(this);
 91         }
 92     }
 93     class Program
 94     {
 95         static void Main(string[] args)
 96         {
 97             Random ra = new Random();
 98             int num = ra.Next(2);
 99             Console.WriteLine(num);
100             PlayerState state = new PlayerState();
101             state.SetState(new Player1());
102             state.SetCards(1);
103             Timer aTimer = new Timer();
104             aTimer.Elapsed += new ElapsedEventHandler(TimedEvent);
105             aTimer.Interval = 1000;    //配置文件中配置的秒数
106             aTimer.Enabled = true;
107             state.Request();
108            // state.Request();
109            // state.Request();
110            // state.Request();
111             Console.Read();
112 
113         }
114        static void TimedEvent(object source, ElapsedEventArgs e)
115         {
116            
117             Console.WriteLine("ok");
118         }
119     }
120 }

状态模式:当一个对象的内在状态改变时,允许改变其行为,这个对象看起来好像改变了其类。状态模式主要解决的是当控制一个对象的状态转换条件表达式过于复杂时的情况。把状态的判断逻辑转移到不同状态的一系列类中,可以把复杂的判断逻辑简化。

posted @ 2014-02-24 13:35  kadajEvo  阅读(112)  评论(0)    收藏  举报