zoukankan      html  css  js  c++  java
  • 状态模式c#(状态流转例子吃饭)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace 状态模式
    {
        public interface State
        {
            void doSomething(Person p);
        }

        public class Mstate : State
        {
            public void doSomething(Person p)
            {
                if (p.hours == 7)
                {
                    Console.WriteLine("吃早餐");
                }
                else
                {
                    p.state = new Lstate();
                    p.doSomething();
                }
            }
        }
            public class Lstate : State
        {
            public void doSomething(Person p)
            {
                if (p.hours == 12)
                {
                    Console.WriteLine("吃午餐");
                }
                else
                {
                    p.state =new Nstate();
                    p.doSomething();
                }
            }
        }
         public class Nstate : State
        {
            public void doSomething(Person p)
            {
                if (p.hours == 18)
                {
                    Console.WriteLine("吃晚餐");
                }
                else
                {
                    p.state =new NOstate();
                    p.doSomething();
                }
            }
        }
            public class NOstate : State
        {
            public void doSomething(Person p)
            {
                Console.WriteLine("没得吃");
              
                 
            }
        }
        public class Person
        {
            public State state { get; set; }

            public int hours { get; set; }

            public Person()
            {
                state=new Mstate();
            }

            public void doSomething()
            {
                state.doSomething(this);
                state=new Mstate();
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                Person p=new Person();
                p.hours = 7;
                p.doSomething();
                p.hours = 12;
                p.doSomething();
                p.hours = 18;
                p.doSomething();
                p.hours = 8;
                p.doSomething();
                p.hours = 7;
                p.doSomething();
                Console.ReadKey();
            }
        }
    }

  • 相关阅读:
    B站崩溃的背后,b站高可用架构到底是怎么样的?
    批量查询注册表键值函数 RegQueryMultipleValues 应用一例
    windows服务程序的安装和卸载函数
    API 获得GetLastError()错误代码对应的文字信息
    API 在屏幕上简单显示字符串
    API 实现类似于 C# DateTime 的类
    Windows API ReportEvent 写系统日志
    .net core 新增对DOCKER后报 ERR_EMPTY_RESPONSE
    Windows docker 安装报 WSL 2 installation is incomplete.
    SSD固态硬盘装系统无法进入引导
  • 原文地址:https://www.cnblogs.com/kexb/p/4525297.html
Copyright © 2011-2022 走看看