设计模式-》状态机模式

例子

  • 我们经常坐电梯都知道,电梯有多种状态,就按最简单的来说,包括运行状态、停止状态、开门状态、闭门状态。下面就以电梯运行为例,举一个具体的实例,UML图如下:
    public abstract class LiftState
    {
        protected LiftContext LiftContext;

        public void SetContext(LiftContext liftContext)
        {
            this.LiftContext = liftContext;
        }

        //电梯门开启动作
        public abstract void Open();

        //电梯门关闭动作
        public abstract void Close();

        //电梯运行
        public abstract void Run();

        //电梯停止
        public abstract void Stop();
    }

    public class OpenState : LiftState
    {
        public override void Open()
        {
            Console.WriteLine("电梯门开启");
        }

        public override void Close()
        {
            LiftContext.SetLiftState(LiftContext.CLOSING_STATE);
            LiftContext.Close();
        }

        public override void Run()
        {
        }

        public override void Stop()
        {
        }
    }

    public class RunState : LiftState
    {
        public override void Open()
        {
        }

        public override void Close()
        {
        }

        public override void Run()
        {
            Console.WriteLine("电梯开始运行");
        }

        public override void Stop()
        {
            LiftContext.SetLiftState(LiftContext.STOPPING_STATE);
            LiftContext.Stop();
        }
    }

    public class StopState : LiftState
    {
        public override void Open()
        {
            LiftContext.SetLiftState(LiftContext.OPENNING_STATE);
            LiftContext.Open();
        }

        public override void Close()
        {
        }

        public override void Run()
        {
            LiftContext.SetLiftState(LiftContext.RUNNING_STATE);
            LiftContext.Run();
        }

        public override void Stop()
        {
            Console.WriteLine("电梯停止了");
        }
    }

    public class CloseState : LiftState
    {
        public override void Open()
        {
            LiftContext.SetLiftState(LiftContext.OPENNING_STATE);
            LiftContext.Open();
        }

        public override void Close()
        {
            Console.WriteLine("电梯门关闭");
        }

        public override void Run()
        {
            LiftContext.SetLiftState(LiftContext.RUNNING_STATE);
            LiftContext.Run();
        }

        public override void Stop()
        {
            LiftContext.SetLiftState(LiftContext.STOPPING_STATE);
            LiftContext.Stop();
        }
    }

    public class LiftContext
    {
        public LiftContext()
        {
            SetLiftState(CLOSING_STATE);
        }

        //定义出电梯的所有状态
        public readonly static LiftState OPENNING_STATE = new OpenState();
        public readonly static LiftState CLOSING_STATE = new CloseState();
        public readonly static LiftState RUNNING_STATE = new RunState();
        public readonly static LiftState STOPPING_STATE = new StopState();

        //定义一个当前电梯状态
        private LiftState LiftState;

        public LiftState GetLiftState()
        {
            return LiftState;
        }

        public void SetLiftState(LiftState liftState)
        {
            this.LiftState = liftState;
            //通知到各个实现类中
            this.LiftState.SetContext(this);
        }

        public void Open()
        {
            this.LiftState.Open();
        }

        public void Close()
        {
            this.LiftState.Close();
        }

        public void Run()
        {
            this.LiftState.Run();
        }

        public void Stop()
        {
            this.LiftState.Stop();
        }
    }

       private static void LiftTest()
        {
            LiftContext liftContext = new LiftContext();
            liftContext.Open();
            liftContext.Close();
            liftContext.Run();
            liftContext.Stop();
        }


设计模式-》状态机模式
参考地址

上一篇:解决IntelljIDEA2021创建项目时没有JAVAEE


下一篇:数据结构与算法—双端队列的实现