Command Pattern 命令模式
Definition
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
命令模式将“请求”封装成对象,以便使用不同的请求、队列或者日志来参数化其他对象,同时支持可撤消的操作。
Participants
The classes and objects participating in this pattern are:
Command
- declares an interface for executing an operation
ConcreteCommand
- defines a binding between a Receiver object and an action
- implements Execute by invoking the corresponding operation(s) on Receiver
Client
- creates a ConcreteCommand object and sets its receiver
Invoker
- asks the command to carry out the request
Receiver
- knows how to perform the operations associated with carrying out the request.
责任链模式代码结构
//-------------------------------------------------------------------------------------
// CommandStructure.cs
//-------------------------------------------------------------------------------------
using UnityEngine;
using System.Collections;
namespace CommandStructure
{
public class CommandStructure : MonoBehaviour
{
void Start ( )
{
// Create receiver, command, and invoker
Receiver receiver = new Receiver();
Command command = new ConcreteCommand(receiver);
Invoker invoker = new Invoker();
// Set and execute command
invoker.SetCommand(command);
invoker.ExecuteCommand();
}
}
/// <summary>
/// The 'Command' abstract class
/// </summary>
abstract class Command
{
protected Receiver receiver;
// Constructor
public Command(Receiver receiver)
{
this.receiver = receiver;
}
public abstract void Execute();
}
/// <summary>
/// The 'ConcreteCommand' class
/// </summary>
class ConcreteCommand : Command
{
// Constructor
public ConcreteCommand(Receiver receiver) :
base(receiver)
{
}
public override void Execute()
{
receiver.Action();
}
}
/// <summary>
/// The 'Receiver' class
/// </summary>
class Receiver
{
public void Action()
{
Debug.Log("Called Receiver.Action()");
}
}
/// <summary>
/// The 'Invoker' class
/// </summary>
class Invoker
{
private Command _command;
public void SetCommand(Command command)
{
this._command = command;
}
public void ExecuteCommand()
{
_command.Execute();
}
}
}
案例1:加减乘除计算器,可以执行撤销 重复 命令,
//-------------------------------------------------------------------------------------
// CommandExample1.cs
//-------------------------------------------------------------------------------------
//This real-world code demonstrates the Command pattern used in a simple calculator with unlimited number of undo's and redo's.
using System;
using UnityEngine;
using System.Collections.Generic;
namespace CommandExample1
{
public class CommandExample1 : MonoBehaviour
{
void Start ( )
{
// Create user and let her compute
User user = new User();
// User presses calculator buttons
user.Compute('+', 100);
user.Compute('-', 50);
user.Compute('*', 10);
user.Compute('/', 2);
// Undo 4 commands
user.Undo(4);
// Redo 3 commands
user.Redo(3);
}
}
/// <summary>
/// The 'Command' abstract class
/// </summary>
abstract class Command
{
public abstract void Execute();
public abstract void UnExecute();
}
/// <summary>
/// The 'ConcreteCommand' class
/// </summary>
class CalculatorCommand : Command
{
private char _operator;
private int _operand;
private Calculator _calculator;
// Constructor
public CalculatorCommand(Calculator calculator,
char operatorSymbol, int operand)
{
this._calculator = calculator;
this._operator = operatorSymbol;
this._operand = operand;
}
// Gets operator
public char Operator
{
set { _operator = value; }
}
// Get operand
public int Operand
{
set { _operand = value; }
}
// Execute new command
public override void Execute()
{
_calculator.Operation(_operator, _operand);
}
// Unexecute last command
public override void UnExecute()
{
_calculator.Operation(Undo(_operator), _operand);
}
// Returns opposite operator for given operator
private char Undo(char operatorSymbol)
{
switch (operatorSymbol)
{
case '+': return '-';
case '-': return '+';
case '*': return '/';
case '/': return '*';
default:
throw new
ArgumentException("UnKnowOperatorSymbol");
}
}
}
/// <summary>
/// The 'Receiver' class
/// </summary>
class Calculator
{
private int _curr = 0;
public void Operation(char operatorSymbol, int operand)
{
switch (operatorSymbol)
{
case '+': _curr += operand; break;
case '-': _curr -= operand; break;
case '*': _curr *= operand; break;
case '/': _curr /= operand; break;
}
Debug.Log("Current value = " + _curr+ " ( following "+ operatorSymbol+operand+" )");
}
}
/// <summary>
/// The 'Invoker' class
/// </summary>
class User
{
// Initializers
private Calculator _calculator = new Calculator();
private List<Command> _commands = new List<Command>();
private int _current = 0;
public void Redo(int levels)
{
Debug.Log("\n---- Redo " + levels + " levels");
for (int i = 0; i < levels; i++)
{
if (_current < _commands.Count - 1)
{
Command command = _commands[_current++];
command.Execute();
}
}
}
public void Undo(int levels)
{
Debug.Log("\n---- Undo "+ levels + " levels");
// Perform undo operations
for (int i = 0; i < levels; i++)
{
if (_current > 0)
{
Command command = _commands[--_current] as Command;
command.UnExecute();
}
}
}
public void Compute(char operatorSymbol, int operand)
{
// Create command operation and execute it
Command command = new CalculatorCommand(
_calculator, operatorSymbol, operand);
command.Execute();
// Add command to undo list
_commands.Add(command);
_current++;
}
}
}