zoukankan      html  css  js  c++  java
  • StatePattern

    class Program
        {
            static void Main(string[] args)
            {
                var state = new OpeningState();
                var lift = new Lift();
                lift.SetListState(state);
                lift.Close();
                lift.Open();
    
                lift.Run();
                lift.Stop();
                Console.Read();
            }
        }
        public abstract class LiftState
        {
            protected Lift Lift;
    
            public void SetLift(Lift lift)
            {
                this.Lift = lift;
            }
            public abstract void Open();
            public abstract void Close();
            public abstract void Run();
            public abstract void Stop();
        }
        public class OpeningState : LiftState
        {
            public override void Open()
            {
                Console.WriteLine("电梯打开!");
            }
    
            public override void Close()
            {
                base.Lift.SetListState(Lift.CloseingState);
                base.Lift.Close();
            }
    
            public override void Run()
            {
                Console.WriteLine("打开不能运行");
            }
    
            public override void Stop()
            {
                base.Lift.SetListState(Lift.StopingState);
                base.Lift.Stop();
            }
        }
        public class ClosingState : LiftState
        {
            public override void Open()
            {
                base.Lift.SetListState(Lift.OpeningState);
                base.Lift.Open();
            }
    
            public override void Close()
            {
                Console.WriteLine("电梯关闭!");
            }
    
            public override void Run()
            {
                base.Lift.SetListState(Lift.RuningState);
                base.Lift.Open();
            }
    
            public override void Stop()
            {
                base.Lift.SetListState(Lift.StopingState);
                base.Lift.Stop();
            }
        }
        public class RuningState : LiftState
        {
            public override void Open()
            {
                base.Lift.SetListState(Lift.OpeningState);
                base.Lift.Open();
            }
    
            public override void Close()
            {
                base.Lift.SetListState(Lift.CloseingState);
                base.Lift.Close();
    
            }
    
            public override void Run()
            {
                Console.WriteLine("电梯运行!");
            }
    
            public override void Stop()
            {
                base.Lift.SetListState(Lift.StopingState);
                base.Lift.Stop();
            }
        }
        public class StopingState : LiftState
        {
            public override void Open()
            {
                base.Lift.SetListState(Lift.OpeningState);
                base.Lift.Open();
            }
    
            public override void Close()
            {
                base.Lift.SetListState(Lift.CloseingState);
                base.Lift.Close();
            }
    
            public override void Run()
            {
                base.Lift.SetListState(Lift.RuningState);
                base.Lift.Open();
            }
    
            public override void Stop()
            {
                Console.WriteLine("电梯停止!");
            }
        }
    
        public class Lift
        {
            public static LiftState OpeningState = new OpeningState();
            public static LiftState CloseingState = new ClosingState();
            public static LiftState RuningState = new RuningState();
            public static LiftState StopingState = new StopingState();
    
            private LiftState _liftState;
            public LiftState GetState()
            {
                return this._liftState;
            }
    
            public void SetListState(LiftState state)
            {
                this._liftState = state;
                this._liftState.SetLift(this);
            }
            public void Open()
            {
                this._liftState.Open();
            }
            public void Close()
            {
                this._liftState.Close();
            }
            public void Stop()
            {
                this._liftState.Stop();
            }
            public void Run()
            {
                this._liftState.Run();
            }
        }
    

      

  • 相关阅读:
    如何在腾讯云上安装Cloud Foundry
    Chrome浏览器扩展程序的本地备份
    如何在Kubernetes里创建一个Nginx service
    如何在Kubernetes里创建一个Nginx应用
    在Mac里给Terminal终端自定义颜色
    linux sed命令详解
    跟我一起写Makefile--- 变量(嵌套变量+追加变量+overrid+多行变量+环境变量+目标变量+模式变量)
    makefile详解 嵌套执行make,定义命令包
    makefile学习笔记(多目录嵌套调用、变量使用)
    Makefile所有内嵌函数
  • 原文地址:https://www.cnblogs.com/since87/p/5331239.html
Copyright © 2011-2022 走看看