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)); } } }