当前位置: 首页 > news >正文

设计模式_状态模式

状态模式

介绍

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

类图

角色: 

stateBase 抽象状态

stateA 具体状态A

stateB 具体状态B

stateC 具体状态C

FactoryState  状态工厂

代码

RoleContext

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


public abstract class StateBase
{public abstract void Action();
}

StateA 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class StateA : StateBase
{string name = "普攻";public override void Action(){Debug.Log("释放-" + name);}
}

StateB 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class StateB : StateBase
{string name = "次元斩";public override void Action(){Debug.Log("释放-" + name);}
}

StateC

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class StateC:StateBase
{string name = "升龙击";public override void Action(){Debug.Log("释放-" + name);}
}

FactoryState

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();elseins = dic[className];}break;case "StateB":{if (false == dic.ContainsKey(className))ins = new StateB();elseins = dic[className];}break;case "StateC":{if (false == dic.ContainsKey(className))ins = new StateC();elseins = dic[className];}break;default:Debug.Log("未发现该类!");break;}return ins;}
}

测试代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class TestZT : MonoBehaviour
{void Start(){RoleContext role = new RoleContext();// 切换技能Arole.ChangeActionState(FactoryState.Instance().GetStateIns("StateA"));role.ShowAction();// 切换技能Brole.ChangeActionState(FactoryState.Instance().GetStateIns("StateB"));role.ShowAction();// 切换技能Crole.ChangeActionState(FactoryState.Instance().GetStateIns("StateC"));role.ShowAction();}}

结果

总结

状态模式

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

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

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

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

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

http://www.lryc.cn/news/213092.html

相关文章:

  • css 某个元素被挤的显示不完整,如何显示完整
  • pve lxc debian 11安装docker遇到bash: sudo: command not解决办法
  • springboot的缓存和redis缓存,入门级别教程
  • 语雀P0级时间爆发,留给运维的时间不多了?
  • LeetCode 2401.最长优雅子数组 ----双指针+位运算
  • NOIP2023模拟6联测27 无穷括号序列
  • java spring cloud 工程企业管理软件-综合型项目管理软件-工程系统源码
  • openEuler 22.03 x86架构下docker运行arm等架构的容器——筑梦之路
  • 【Java】HashMap常见的面试题
  • openpnp - src - 配置文件载入过程的初步分析
  • 中国各城市土地利用类型(城市功能)数据集(shp)
  • Linux网络编程:数据链路层
  • python 线程 超时时间
  • LeetCode:274. H 指数、275. H 指数 II(C++)
  • 多线程及锁
  • C++ 写一个Data类的注意问题
  • postman做接口测试
  • hdlbits系列verilog解答(always块)-29
  • uniapp实现瀑布流
  • 15. 机器学习 - 支持向量机
  • 如何根据进程号查询服务的端口号
  • 2.10、自定义量化优化过程
  • MySQL如何添加自定义函数
  • 超融合数据库:解锁全场景数据价值的钥匙
  • Pap.er for Mac:高清壁纸应用打造你的专属视觉盛宴
  • AI:46-基于深度学习的垃圾邮件识别
  • 【骑行贝丘渔场】一场与海的邂逅,一段难忘的旅程
  • 消息中间件——RabbitMQ(一)Windows/Linux环境搭建(完整版)
  • Mysql 表读锁与表写锁
  • 目标检测概述