Unity --- game design patterns (13) of the command mode



See the overview Reference Reference blog

Encapsulate a request to a Command object, so you to client requests using different parameterization; request queue or log requests, and support undoable operations.

For example, base upgrade feature RTS game. Upgrade takes time, increases several times when we upgrade, it will wait before performing an upgrade behind after the first upgrade is complete.
At this point if we want to reduce the number of times you can upgrade.
We can upgrade the operation of each such request as a Command, each request to wait in line to perform, we can withdraw the request.

1, the command mode prototype

FIG Command Mode Prototype UML

Command Mode prototype code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 命令模式
/// </summary>
public class CommandMode : MonoBehaviour
{
    private void Start()
    {
        CommandInvoker invoker = new CommandInvoker();
        ConcreteCommand1 cmd1 = new ConcreteCommand1(new Receiver1());
        ConcreteCommand1 cmd2 = new ConcreteCommand1(new Receiver1());

        invoker.AddCommand(cmd1);
        invoker.AddCommand(cmd2);
        invoker.ExecuteCommand();

    }
}


/// <summary>
/// 命令管理类
/// 根据invoker执行Command命令
/// </summary>
public class CommandInvoker
{
    //命令集合
    private List<ICommand> mCommands = new List<ICommand>();

    public void AddCommand(ICommand command)
    {
        mCommands.Add(command);
    }
    /// <summary>
    /// 使用params参数一次添加多个命令
    /// </summary>
    public void AddCommand(params ICommand[] commands)
    {
        foreach (ICommand command in commands)
        {
            mCommands.Add(command);
        }
    }

    /// <summary>
    /// 执行所有命令,执行完并清除
    /// </summary>
    public void ExecuteCommand()
    {
        foreach (ICommand command in mCommands)
        {
            command.Execute();
        }
        mCommands.Clear();
    }
}

/// <summary>
/// 抽象命令类
/// </summary>
public abstract class ICommand
{
    public abstract void Execute();
}
public class ConcreteCommand1 : ICommand
{
    //每个命令类中有一个接收者,接收者对象绑定一个具体的动作
    private Receiver1 mReceiver1;

    public ConcreteCommand1(Receiver1 receiver1)
    {
        mReceiver1 = receiver1;
    }

    //通过调用接收者来执行具体的命令
    public override void Execute()
    {
        mReceiver1.Action("ConcreteCommand1");
    }
}

/// <summary>
/// 接收者类,绑定一个具体的执行操作,任何类都可能作为一个接收者
/// </summary>
public class Receiver1
{
    public void Action(string cmd)
    {
        Debug.Log("Receiver1执行了命令:" + cmd);
    }
}

2, advantages and disadvantages of command mode

advantage

  1. Reducing the coupling between the requester and the implementer.
  2. A request queue or log requests, operation support revocation.
  3. A combination can be easily designed command.
  4. New commands can be easily added to the system.

Shortcoming

  1. The number of class more

Guess you like

Origin www.cnblogs.com/Fflyqaq/p/11722396.html