# 状态模式

属于行为型模式。
允许对象在内部状态发生改变时改变它的行为,对象看起来好像修改了它的类。
对象的行为依赖于它的状态(属性),并且可以根据它的状态改变而改变它的相关行为。

在面临大量的 if else 语句时,使用状态模式可以消除这些 if else 语句,并且可以封装变化点,新增条件和状态时,只需要在原来的基础上进行扩展即可。

有限状态机中使用了这种模式。

# 类图

img

# 代码

# 状态类

namespace BehavioralPatterns_StatePattern
{
    /// <summary>
    /// 状态类
    /// </summary>
    public abstract class State
    {
        /// <summary>
        /// 触发条件
        /// </summary>
        public ITrigger Trigger;
        /// <summary>
        /// 进入状态
        /// </summary>
        public abstract void EnterState();
    }
}
namespace BehavioralPatterns_StatePattern
{
    public class State_1 : State
    {
        public State_1()
        {
            Trigger = new Trigger_1();
        }
        public override void EnterState()
        {
            Console.WriteLine("State_1");
        }
    }
}
namespace BehavioralPatterns_StatePattern
{
    public class State_0 : State
    {
        public State_0()
        {
            Trigger = new Trigger_0();
        }
        public override void EnterState()
        {
            Console.WriteLine("State_0");
        }
    }
}

# 条件类

namespace BehavioralPatterns_StatePattern
{
    /// <summary>
    /// 条件接口
    /// </summary>
    public interface ITrigger
    {
        /// <summary>
        /// 条件触发就切换状态
        /// </summary>
        /// <returns></returns>
        public State HandleTrigger(StateController control);
    }
}
namespace BehavioralPatterns_StatePattern
{
    public class Trigger_0 : ITrigger
    {
        public State HandleTrigger(StateController control)
        {
            if (control.Time >= 10)
            {
                return new State_1();
            }
            return control.CurrentState;
        }
    }
}
namespace BehavioralPatterns_StatePattern
{
    public class Trigger_1 : ITrigger
    {
        public State HandleTrigger(StateController control)
        {
            if (control.Time < 10)
            {
                return new State_0();
            }
            return control.CurrentState;
        }
    }
}

# 状态控制类

namespace BehavioralPatterns_StatePattern
{
    /// <summary>
    /// 状态控制类
    /// </summary>
    public class StateController
    {
        /// <summary>
        /// 当前的状态
        /// </summary>
        public State CurrentState;
        /// <summary>
        /// 状态的切换条件
        /// </summary>
        public ITrigger CurrentTrigger;
        /// <summary>
        /// 用于切换状态
        /// </summary>
        public float Time;
        public StateController()
        {
            // 设置默认状态
            Time = 0;
            CurrentState = new State_0();
            CurrentTrigger = CurrentState.Trigger;
        }
        /// <summary>
        /// 执行当前状态的操作
        /// </summary>
        public void StateAction()
        {
            CurrentState.EnterState();
        }
        /// <summary>
        /// 用线程检测条件的触发
        /// </summary>
        public void ChangeState()
        {
            while (true)
            {
                CurrentState = CurrentTrigger.HandleTrigger(this);
                CurrentTrigger = CurrentState.Trigger;
                Thread.Sleep(100);
            }
        }
    }
}

# 测试

namespace BehavioralPatterns_StatePattern
{
    public class Program
    {
        private static void Main()
        {
            StateController stateController = new StateController();
            Thread thread = new Thread(stateController.ChangeState);
            thread.Start();
            stateController.StateAction();
            stateController.Time = 12;
            Thread.Sleep(500);
            stateController.StateAction();
            thread.Abort();
        }
    }
}

运行结果:
State_0
State_1

更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

Maikire 微信支付

微信支付

Maikire 支付宝

支付宝

Maikire 贝宝

贝宝