Unity实现设计模式——状态模式

Unity实现设计模式------状态模式

状态模式最核心的设计思路就是将对象的状态抽象出一个接口,然后根据它的不同状态封装其行为,这样就可以实现状态和行为的绑定,最终实现对象和状态的有效解耦。

在实际开发中一般用到FSM有限状态机的实现,GF框架中的FSM和流程控制就是基于这个原理实现的。

1.State(状态的抽象基类)

csharp 复制代码
    public abstract class State
    {
        protected Context m_Context = null;

        public State(Context theContext)
        {
            m_Context = theContext;
        }
        public abstract void Handle(int Value);
    }

2.ConcreteStateA,ConcreteStateB,ConcreteStateC

状态State的子类

csharp 复制代码
    /// <summary>
    /// 状态A
    /// </summary>
    public class ConcreteStateA : State
    {
        public ConcreteStateA(Context theContext) : base(theContext)
        { }

        public override void Handle(int Value)
        {
            Debug.Log("ConcreteStateA.Handle");
            if (Value > 10)
                m_Context.SetState(new ConcreteStateB(m_Context));
        }

    }

    /// <summary>
    /// 状态B
    /// </summary>
    public class ConcreteStateB : State
    {
        public ConcreteStateB(Context theContext) : base(theContext)
        { }

        public override void Handle(int Value)
        {
            Debug.Log("ConcreteStateB.Handle");
            if (Value > 20)
                m_Context.SetState(new ConcreteStateC(m_Context));
        }

    }

    /// <summary>
    /// 状态C
    /// </summary>
    public class ConcreteStateC : State
    {
        public ConcreteStateC(Context theContext) : base(theContext)
        { }

        public override void Handle(int Value)
        {
            Debug.Log("ConcreteStateC.Handle");
            if (Value > 30)
                m_Context.SetState(new ConcreteStateA(m_Context));
        }
    }

3.Context

Context类-持有目前的状态,并将相关信息传给状态

csharp 复制代码
    public class Context
    {
        State m_State = null;

        public void Request(int Value)
        {
            m_State.Handle(Value);
        }

        public void SetState(State theState)
        {
            Debug.Log("Context.SetState:" + theState);
            m_State = theState;
        }
    }

4.测试代码

csharp 复制代码
    public class StatePatternExample5 : MonoBehaviour
    {
        void Start()
        {
            UnitTest();
        }

        void UnitTest()
        {
            Context theContext = new Context();
            theContext.SetState(new ConcreteStateA(theContext));
            theContext.Request(5);
            theContext.Request(15);
            theContext.Request(25);
            theContext.Request(35);

        }
    }
相关推荐
工业甲酰苯胺4 小时前
TypeScript枚举类型应用:前后端状态码映射的最简方案
javascript·typescript·状态模式
死也不注释7 小时前
【第零章编辑器开发与拓展】
unity·编辑器
死也不注释8 小时前
【第一章编辑器开发基础第二节编辑器布局_3GUI元素和布局大小(3/4)】
unity·编辑器
UI设计和前端开发从业者9 小时前
数字孪生技术驱动UI前端革新:实现产品设计的虚拟仿真与实时反馈
状态模式
绅士玖9 小时前
JavaScript 设计模式之单例模式🚀
前端·javascript·设计模式
永卿00111 小时前
设计模式-门面模式
设计模式
凤山老林11 小时前
Spring Boot中的中介者模式:终结对象交互的“蜘蛛网”困境
java·spring boot·后端·设计模式·中介者模式
找了一圈尾巴13 小时前
设计模式(结构型)-适配器模式
设计模式·适配器模式
vvilkim13 小时前
单例模式详解:确保一个类只有一个实例
单例模式·设计模式
CodeWithMe13 小时前
【读书笔记】《C++ Software Design》第六章深入剖析 Adapter、Observer 和 CRTP 模式
c++·设计模式