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

设计模式 --- 命令模式

命令模式是一种行为设计模式,它将请求封装成一个对象,从而允许你使用不同的请求、队列或日志来参数化其他对象,同时支持请求的撤销与恢复。

优点:

1.解耦调用者与接收者​​:输入处理无需直接调用角色方法
2.支持撤销和恢复:通过实现撤销命令,可以方便地实现操作的撤销和恢复功能。
​​3.扩展性强​​:新增命令不影响现有代码

缺点:

1.​​类数量膨胀​:使用命令模式时,每一个具体的操作都需要创建一个具体的命令类。如果系统中有大量不同的操作,那么就会产生大量的命令类。
优化:1.使用​​泛型命令基类​​   2.通过委托/Lambda​​ 简化简单命令
2.​​内存占用高​:为了支持撤销和恢复操作,需要保存大量的命令对象,这会占用大量的内存空间。
优化:1.实现​​命令对象池​​     2.使用 ​​结构体命令​​(适用于简单场景)
3.​​撤销实现复杂​:
优化:1.快照模式保存状态   2.增量式撤销(仅记录状态变化)

说明例子:

1.UML类图:

2.实现:

1.定义接收者:

public class Receiver1
{
    public void Action(string Command)
    {
        Debug.Log("Receiver1.Action: Command['+Command+']");
    }
}

public class Receiver2
{
    public void Action(int Param)
    {
        Debug.Log("Receiver2.Action: Param['+Param.ToString()+']");
    }
}

2.定义命令基类:

public abstract class Command
{
    public abstract void Execute();
}

3.定义具体命令类:

public class ConcreteCommad1 : Command
{
    Receiver1 m_Receiver1 = null;
    string m_Command = "";

    public ConcreteCommad1(Receiver1 receiver1,string command)
    {
        this.m_Receiver1 = receiver1;
        this.m_Command = command;
    }

    public override void Execute()
    {
        this.m_Receiver1.Action(m_Command);
    }
}

 4.定义调用者:

public class Invoker
{
    List<Command> m_Commands = new List<Command>();

    //加入命令
    public void AddCommand(Command theCommand)
    {
        this.m_Commands.Add(theCommand);
    }

    //执行命令
    public void ExecuteCommand()
    {
        //执行
        foreach (var commandItem in m_Commands)
        {
            commandItem.Execute();
        }
        //清空
        m_Commands.Clear();
    }
}

5.测试类: 


public class CommandPattern : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Invoker invoker = new Invoker();

        //将命令与执行结合
        Command theCommand = null;
        theCommand = new ConcreteCommad1(new Receiver1(),"Hello");
        invoker.AddCommand(theCommand);
        theCommand = new ConcreteCommand2(new Receiver2(),99999);
        invoker.AddCommand(theCommand);

        //执行
        invoker.ExecuteCommand();
    }
}

游戏中使用场景:

​​1.输入处理​​:将玩家操作(移动、攻击)封装为命令,支持不同设备输入映射。
​​2.技能/技能系统​​:实现技能释放、取消和连招组合。
​​3.撤销/重做系统​​:支持玩家撤销建造操作或技能释放。
4.​​AI指令队列​​:管理AI行为的顺序执行(巡逻→攻击→撤退),将AI节点行为封装为可回放的命令序列。
5.​​网络同步​​:将玩家操作序列化为命令对象进行网络传输。

总结:

在游戏开发中,命令模式特别适合需要灵活控制操作执行流程的场景​​,如技能系统、输入处理和AI指令管理。通过合理优化,可在保证灵活性的同时兼顾性能。

参考书籍:

《Hands-On Game Development Patterns with Unity 2019》

《设计模式与游戏完美开发》


文章转载自:

http://umcCU2v8.LLthz.cn
http://VmuNex00.LLthz.cn
http://9fipo65k.LLthz.cn
http://IuWXBxk7.LLthz.cn
http://UYXmfZBZ.LLthz.cn
http://oznyfxTM.LLthz.cn
http://jaXPaiCt.LLthz.cn
http://YVzpWlHh.LLthz.cn
http://D65Tv7BG.LLthz.cn
http://YQnxlaR7.LLthz.cn
http://qYyQK31b.LLthz.cn
http://L2fOVcpm.LLthz.cn
http://qV1smtpf.LLthz.cn
http://agD8m9XN.LLthz.cn
http://cLwLrqBc.LLthz.cn
http://gRdRsRDq.LLthz.cn
http://j6aYCPBu.LLthz.cn
http://ItpkdVzK.LLthz.cn
http://XXkBVoxR.LLthz.cn
http://YBRZxC8N.LLthz.cn
http://SBOwnOmd.LLthz.cn
http://kfpqS36U.LLthz.cn
http://p1yeNsFJ.LLthz.cn
http://llXgSXmY.LLthz.cn
http://IDEq3Jb1.LLthz.cn
http://reewdt9S.LLthz.cn
http://sZPoJ8fc.LLthz.cn
http://QMi0CTkY.LLthz.cn
http://rZw9S8Bt.LLthz.cn
http://b2DJQ8zY.LLthz.cn
http://www.dtcms.com/a/127862.html

相关文章:

  • C++进阶——C++11_{ }初始化_lambda_包装器
  • Flutter常用组件实践
  • python+requests接口自动化测试框架实例教程
  • C#容器源码分析 --- Queue<T>
  • 2025届蓝桥杯JavaB组个人题解(暂时不全,没题目)
  • 【AI】AI大模型发展史:从理论探索到技术爆发
  • [创业之路-366]:投资尽职调查 - 尽调核心逻辑与核心影响因素:价值、估值、退出、风险、策略
  • webpack vite
  • 基于 Termux 在移动端配置 Ubuntu 系统并搭建工作环境
  • DeepSeek在应急救援领域的应用解决方案
  • docker测试镜像源
  • 如何在运行时获取硬件信息
  • day24 学习笔记
  • Linux:35.其他IPC和IPC原理+信号量入门
  • 自动驾驶的数据集以及yolov8和yolop
  • Oracle 复制表结构(含索引、主键)操作指南
  • 池式结构---内存池
  • 企业年报问答RAG挑战赛冠军方案:从零到SotA,一战封神
  • AI 大语言模型 (LLM) 平台的整体概览与未来发展
  • #关于数据库中的时间存储
  • 006.Gitlab CICD流水线触发
  • Python实现链接KS3,并批量下载KS3文件数据到本地
  • MySQL数据库备份与恢复详解
  • 21 天 Python 计划:MySQL索引机制从基础到应用
  • 人事招聘专员简历模板
  • 谷歌开源代理开发工具包(Agent Development Kit,ADK):让多智能体应用的构建变得更简
  • 【区分定语从句和同位语从句】
  • 普瑞PS8742B
  • 【JavaScript】对 Proxy 与 defineProperty 的理解和运用场景
  • DeepSeek大语言模型部署指南:从基础认知到本地实现