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

        }
    }
相关推荐
Damon小智27 分钟前
鸿蒙元服务深度实践:跨端唤醒与状态共享的设计模式
华为·设计模式·harmonyos
shaominjin12328 分钟前
单例模式:设计模式中的“独一无二“之道
android·单例模式·设计模式
地狱为王4 小时前
Unity使用AnimeGANv3实现动漫风格化效果(二)
unity·游戏引擎
fcm194 小时前
unity之线框模式
unity·游戏引擎
unity工具人4 小时前
unity DoTween DoPath设置物体按照指定轨迹运动
unity·游戏引擎
程序猿阿伟4 小时前
《风格锚点+动态适配:Unity跨设备渲染的核心逻辑》
unity·游戏引擎
欠你一个bug5 小时前
Java设计模式应用--装饰器模式
java·设计模式·装饰器模式
LoveXming10 小时前
Chapter14—中介者模式
c++·microsoft·设计模式·中介者模式·开闭原则
崎岖Qiu12 小时前
【设计模式笔记06】:单一职责原则
java·笔记·设计模式·单一职责原则
Yeniden12 小时前
【设计模式】适配器模式大白话讲解!
设计模式·适配器模式