设计模式_状态模式

状态模式

介绍

|------|------------------------------|--------------------------------------|------------------------|---------------------------|
| 设计模式 | 定义 | 案例 | 问题堆积在哪里 | 解决办法 |
| 状态模式 | 一个对象 状态可以发生改变 不同的状态又有不同的行为逻辑 | 游戏角色 加载不同的技能 每个技能有不同的:攻击逻辑 攻击范围 动作等等 | 1 状态很多 2 每个状态有自己的属性和逻辑 | 每种状态单独写一个类 角色需要那个状态就加载哪一个 |

类图

角色:

stateBase 抽象状态

stateA 具体状态A

stateB 具体状态B

stateC 具体状态C

FactoryState 状态工厂

代码

RoleContext

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RoleContext
{
    string name;
    StateBase currentState = null;

    public RoleContext()
    {
        name = "独孤求败";
    }

    // 展示动作
    public void ShowAction()
    {
        if (null != currentState)
        {
            currentState.Action();
        }
    }

    // 切换状态
    public void ChangeActionState(StateBase newState)
    {
        currentState = newState;
    }
}

StateBase

cs 复制代码
public abstract class StateBase
{
    public abstract void Action();
}

StateA

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StateA : StateBase
{
    string name = "普攻";

    public override void Action()
    {
        Debug.Log("释放-" + name);
    }
}

StateB

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StateB : StateBase
{
    string name = "次元斩";

    public override void Action()
    {
        Debug.Log("释放-" + name);
    }
}

StateC

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StateC:StateBase
{
    string name = "升龙击";

    public override void Action()
    {
        Debug.Log("释放-" + name);
    }
}

FactoryState

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FactoryState
{
    // 单例
    static FactoryState  self = null;
    FactoryState() { }
    public static  FactoryState Instance()
    {
        if (null == self)
            self = new FactoryState();
        return self;
    }


    Dictionary<string, StateBase> dic = new Dictionary<string, StateBase>();

    // 获取实例
    public StateBase GetStateIns(string className)
    {
        StateBase ins = null;

        switch (className)
        {
            case "StateA":
                {
                    if (false == dic.ContainsKey(className))
                        ins = new StateA();
                    else
                        ins = dic[className];
                }
                break;

            case "StateB":
                {
                    if (false == dic.ContainsKey(className))
                        ins = new StateB();
                    else
                        ins = dic[className];
                }
                break;

            case "StateC":
                {
                    if (false == dic.ContainsKey(className))
                        ins = new StateC();
                    else
                        ins = dic[className];
                }
                break;

            default:
                Debug.Log("未发现该类!");
                break;
        }

        return ins;
    }
}

测试代码

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestZT : MonoBehaviour
{
    void Start()
    {
        RoleContext role = new RoleContext();

        // 切换技能A
        role.ChangeActionState(FactoryState.Instance().GetStateIns("StateA"));
        role.ShowAction();

        // 切换技能B
        role.ChangeActionState(FactoryState.Instance().GetStateIns("StateB"));
        role.ShowAction();

        // 切换技能C
        role.ChangeActionState(FactoryState.Instance().GetStateIns("StateC"));
        role.ShowAction();

    }

}

结果

总结

状态模式

1 是一个非常好用的 解耦合的手段, 角色不同的状态封装不同的(动作,属性,限制)

2 非常符合对修改封闭对扩展开发的原则。

3 让状态的职责更加的单一

4 也符合了依赖倒置 ,依赖了抽象

5 符合了迪米特原则,角色对状态类内部的具体实现不知道

相关推荐
wu~9703 分钟前
手撕四种常用设计模式(工厂,策略,代理,单例)
java·单例模式·设计模式·代理模式·抽象工厂模式·策略模式
Yvonne爱编码6 小时前
CSS- 4.4 固定定位(fixed)& 咖啡售卖官网实例
前端·css·html·状态模式·hbuilder
敲代码的 蜡笔小新7 小时前
【行为型之访问者模式】游戏开发实战——Unity灵活数据操作与跨系统交互的架构秘诀
unity·设计模式·c#·访问者模式
软考真题app20 小时前
软件设计师考试结构型设计模式考点全解析
设计模式·软件设计师·结构型设计模式·考试考点
Yvonne爱编码1 天前
CSS- 4.2 相对定位(position: relative)
前端·css·状态模式·html5·hbuilder
鸡吃丸子1 天前
常见的实时通信技术(轮询、sse、websocket、webhooks)
前端·websocket·状态模式
xiaolin03331 天前
【设计模式】- 行为型模式1
设计模式·状态模式·责任链模式·策略模式·命令模式·模板方法模式·行为型模式
沐土Arvin1 天前
深入理解 requestIdleCallback:浏览器空闲时段的性能优化利器
开发语言·前端·javascript·设计模式·html
bao_lanlan1 天前
兰亭妙微:用系统化思维重构智能座舱 UI 体验
ui·设计模式·信息可视化·人机交互·交互·ux·外观模式
总是难免1 天前
设计模式 - 单例模式 - Tips
java·单例模式·设计模式