• 设计模式》状态机模式


    例子

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

  • 相关阅读:
    字符串匹配的KMP算法
    附加数据库出错
    SQL Server 2005
    OpenFileDialog对话框Filter属性
    flink run 报错java.lang.NoSuchMethodError: org.apache.hadoop.ipc.Client.getRpcTimeout(Lorg/apache/hadoop/conf/Configuration;)I
    修改cdh5集群中主机节点IP或hostName
    oracle 自增序列 在 mybatis中应用
    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
    java远程连接hadoop,kerbers认证失败 报no supported default etypes for default_tkt_enctypes
    hive distribute by sort by 去重
  • 原文地址:https://www.cnblogs.com/icxldd/p/15804920.html
Copyright © 2020-2023  润新知