状态模式
Posted SpringDragon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了状态模式相关的知识,希望对你有一定的参考价值。
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)); } } }
以上是关于状态模式的主要内容,如果未能解决你的问题,请参考以下文章