Unity23种设计模式之 命令模式

在项目中,会经常遇到这有的问题:

UI按钮点击,直接在onclick里写了一堆逻辑。

输入系统和角色行为强耦合。

想做撤销、重做、回放、AI自动操作时,发现代码全部绑定死了。

这时候,命令模式非常香了。

1.什么是命令模式?

将"请求"封装成对象,从而让你可以用不同的请求来参数化客户端,支持排队、日志、撤销等操作。

UML结构简化

Invoker(调用者) → Command(命令接口) → Receiver(执行者)

Invoke:按钮、输入系统、AI

Command:一次具体行为(Jump,Attack,UseItem)

Receiver:真正干活的对象(Player、Enemy、System)

2.为什么Unity项目特别适合命令模式?

(1)

cs 复制代码
public void OnJumpBtnClick()
{
    player.Jump();
}

耦合严重:UI必须知道Player,该逻辑要改UI,没法复用。

(2)支持撤销重做,比如回合制、编辑器工具、策略卡牌游戏。

(3)AI、自动操作、回放系统必备。

3.命令模式基础实现

(1)Command抽象接口

cs 复制代码
public interface ICommand
{
    void Execute();
    void Undo(); // 可选
}

(2)Receiver(真正执行逻辑的对象)

cs 复制代码
public class Player
{
    public void Jump()
    {
        Debug.Log("Player Jump");
    }

    public void Crouch()
    {
        Debug.Log("Player Crouch");
    }
}

(3)Concrete Command(具体命令)

cs 复制代码
public class JumpCommand : ICommand
{
    private Player _player;

    public JumpCommand(Player player)
    {
        _player = player;
    }

    public void Execute()
    {
        _player.Jump();
    }

    public void Undo()
    {
        // 跳跃一般不能撤销,这里可以留空
    }
}

public class CrouchCommand : ICommand
{
    private Player _player;

    public CrouchCommand(Player player)
    {
        _player = player;
    }

    public void Execute()
    {
        _player.Crouch();
    }

    public void Undo()
    {
        Debug.Log("Undo Crouch");
    }
}

(4)Invoker(调用者)

cs 复制代码
public class InputHandler
{
    private ICommand _jumpCommand;
    private ICommand _crouchCommand;

    public InputHandler(Player player)
    {
        _jumpCommand = new JumpCommand(player);
        _crouchCommand = new CrouchCommand(player);
    }

    public void HandleInput()
    {
        if (Input.GetKeyDown(KeyCode.Space))
            _jumpCommand.Execute();

        if (Input.GetKeyDown(KeyCode.C))
            _crouchCommand.Execute();
    }
}

4.UI绑定+命令模式

cs 复制代码
public class UIButtonInvoker : MonoBehaviour
{
    private ICommand _command;

    public void SetCommand(ICommand command)
    {
        _command = command;
    }

    public void OnClick()
    {
        _command?.Execute();
    }
}


//绑定时
var button = jumpBtn.GetComponent<UIButtonInvoker>();
button.SetCommand(new JumpCommand(player));

5.命令队列

cs 复制代码
public class CommandQueue
{
    private Queue<ICommand> _queue = new Queue<ICommand>();

    public void Add(ICommand command)
    {
        _queue.Enqueue(command);
    }

    public void ExecuteAll()
    {
        while (_queue.Count > 0)
        {
            _queue.Dequeue().Execute();
        }
    }
}

6.撤销重做

cs 复制代码
public class CommandHistory
{
    private Stack<ICommand> _undoStack = new Stack<ICommand>();

    public void Execute(ICommand command)
    {
        command.Execute();
        _undoStack.Push(command);
    }

    public void Undo()
    {
        if (_undoStack.Count > 0)
            _undoStack.Pop().Undo();
    }
}
相关推荐
Zane19945 天前
一个 new 就能创建对象,为什么还要拆出单例、工厂、建造者、原型四种模式?
设计模式
怕浪猫5 天前
一行命令复刻爆款视频,我把 Hypit 从安装跑到了出片
人工智能·设计模式·程序员
Zane19946 天前
函数式编程里的函数,其实不是你天天写的那个函数——三大编程范式的边界在哪
设计模式
Zane19947 天前
策略模式现在该不该上?一次讲清楚过度设计和设计不足怎么找平衡
设计模式
她说..7 天前
常见设计模式-模板方法模式
java·spring·设计模式·springboot
xiaofeiyang1507 天前
第六章 · 桥接 — 三支毛笔,画出九种颜色
设计模式
Shadow(⊙o⊙)8 天前
OTOL设计模式 One Thread One Loop
服务器·网络·设计模式
sarasuki8 天前
如何让LLM 能在半夜偷偷打开网易云呢?
人工智能·设计模式·agent
小王师傅668 天前
【设计模式】装饰模式(四):框架源码实战——从 Java I/O 到 Spring 到 MyBatis
java·设计模式
执明wa9 天前
Android RecyclerView 多类型, 多种 Item
android·xml·开发语言·设计模式·android studio