设计模式_状态模式

状态模式

介绍

|------|------------------------------|--------------------------------------|------------------------|---------------------------|
| 设计模式 | 定义 | 案例 | 问题堆积在哪里 | 解决办法 |
| 状态模式 | 一个对象 状态可以发生改变 不同的状态又有不同的行为逻辑 | 游戏角色 加载不同的技能 每个技能有不同的:攻击逻辑 攻击范围 动作等等 | 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 符合了迪米特原则,角色对状态类内部的具体实现不知道

相关推荐
William_cl6 小时前
【CSDN 专栏】C# ASP.NET控制器过滤器:自定义 ActionFilterAttribute 实战(避坑 + 图解)
c#·asp.net·状态模式
柯南二号7 小时前
【后端】【Java】一文详解Spring Boot RESTful 接口统一返回与异常处理实践
java·spring boot·状态模式·restful
fakerth1 天前
【OpenHarmony】设计模式模块详解
c++·单例模式·设计模式·openharmony
alibli1 天前
一文学会设计模式之创建型模式及最佳实现
c++·设计模式
1024肥宅1 天前
前端常用模式:提升代码质量的四大核心模式
前端·javascript·设计模式
张毫洁1 天前
将后端resources中的文件返给前端下载的方法
前端·状态模式
郝学胜-神的一滴1 天前
设计模式依赖于多态特性
java·开发语言·c++·python·程序人生·设计模式·软件工程
帅次1 天前
系统分析师:软件需求工程的软件需求概述、需求获取、需求分析
设计模式·重构·软件工程·团队开发·软件构建·需求分析·规格说明书
EXtreme351 天前
【数据结构】算法艺术:如何用两个栈(LIFO)优雅地模拟队列(FIFO)?
c语言·数据结构·算法·设计模式·栈与队列·摊还分析·算法艺术
阿里巴啦2 天前
从零搭建移动端数字人生成应用:React + Go + D‑ID 实战
react.js·golang·状态模式·数字人·did·ai移动端数字人