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


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

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

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

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

        public abstract class State
        {
            protected Context m_Context = null;
    
            public State(Context theContext)
            {
                m_Context = theContext;
            }
            public abstract void Handle(int Value);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2.ConcreteStateA,ConcreteStateB,ConcreteStateC

    状态State的子类

        /// 
        /// 状态A
        /// 
        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));
            }
    
        }
    
        /// 
        /// 状态B
        /// 
        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));
            }
    
        }
    
        /// 
        /// 状态C
        /// 
        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));
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    3.Context

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

        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;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    4.测试代码

        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);
    
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    Kafka在企业级应用中的实践
    地级市高新技术企业统计情况(2000-2019)
    Electron如何在UOS操作系统(统信)下打包成桌面应用?
    从零开始C语言精讲篇7:数据的存储
    U2-Net显著性检测源码详解
    [UEFI]SHELL命令汇总
    【JavaSE】抽象类与接口
    写一个函数实现:将一个5*5的矩阵中最大的元素放在中心,4个角分别放4个最小的元素(顺序为从左到右,从上到下依次从小到大存放)之解法改写
    手写简易Spring框架
    Python之第十章 IO及对象列化
  • 原文地址:https://blog.csdn.net/zzzsss123333/article/details/133339693