zoukankan      html  css  js  c++  java
  • 状态模式

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class StateDesignMode01 : MonoBehaviour
    {
        private void Start()
        {
            Context context = new Context();
            context.SetState(new ConcreteStateA(context));
            context.Handle(5);
            context.Handle(20);
        }
    }
    
    public class Context
    {
        private IState mState;
        public void SetState(IState state)
        {
            mState = state;
        }
        public void Handle(int arg)
        {
            mState.Handle(arg);
        }
    }
    
    public interface IState
    {
        void Handle(int arg);
    }
    public class ConcreteStateA : IState
    {
        private Context mContext;
        public ConcreteStateA(Context context)
        {
            mContext = context;
        }
        public void Handle(int arg)
        {
            Debug.Log("ConcreteStateA.Handle" + arg);
            if (arg > 10)
            {
                //状态切换到B
                mContext.SetState(new ConcreteStateB(mContext));
            }
        }
    }
    public class ConcreteStateB : IState
    {
        private Context mContext;
        public ConcreteStateB(Context context)
        {
            mContext = context;
        }
        public void Handle(int arg)
        {
            Debug.Log("ConcreteStateB.Handle" + arg);
            if (arg <= 10)
            {
                //状态切换到A
                mContext.SetState(new ConcreteStateA(mContext));
            }
        }
    }
    

      

  • 相关阅读:
    react native
    快速幂模板
    Java异常归纳
    Java环境变量配置
    过滤器
    cookie和session页面随机数和防止重复提交
    javabean&el&jstl
    servlet&jsp
    Tomcat和Servlet入门
    网络编程
  • 原文地址:https://www.cnblogs.com/xingyunge/p/7718754.html
Copyright © 2011-2022 走看看