• 设计模式》状态机模式


    例子

    • 我们经常坐电梯都知道,电梯有多种状态,就按最简单的来说,包括运行状态、停止状态、开门状态、闭门状态。下面就以电梯运行为例,举一个具体的实例,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();
            }
    
    
    

    UML
    参考地址

  • 相关阅读:
    背包问题
    標準差、方差、正太分佈公式
    C#實現XML的增刪查改
    XML的基礎結構
    微信小程序入門學習資料鏈接
    微信小程序wxml無法實現頁面跳轉的問題
    088_jsp转成成servle所在的路径?
    075_jsp如何debug?
    028_Mybatis返回int类型为空时报错 attempted to return null from a method with a primitive return type (int)
    087_数据库类型
  • 原文地址:https://www.cnblogs.com/icxldd/p/15804920.html
Copyright © 2020-2023  润新知