UGTK cat mascotUGTKengine within an engine Request access

UGTK / Toolkit / Design Patterns / Command Completed

Design Patterns — UGTK

Command Pattern

Summary

The Command Pattern is a behavioral design pattern that turns a request into a stand-alone object that contains all the information about the request.

This transformation lets you pass requests as a method argument, delay or queue a request's execution, and support undoable operations.


Content


Modules Dependencies

The Command Pattern does not have any dependencies on other modules.


Setup

If you use Mn_CommandInvoker (Optional):

Make a component inherit from Mn_CommandInvoker to execute commands.

public class Mn_GameCommandInvoker : Mn_CommandInvoker<GameCommandInvoker, IGameCommand> {}

GameCommandInvoker is the class that inherits from ICommandInvoker. IGameCommand is the interface that inherits from ICommandBase.

If you use pure class:

Create a class that inherits from ICommandInvoker.

    public class GameCommandInvoker : CommandInvoker<IGameCommand> {}

IGameCommand is the interface that inherits from ICommandBase.


How To Use

1. Create a Command:

public class GameCommand : IGameCommand
{
    public void Execute()
    {
        // Do something
    }

    public void Undo()
    {
        // Undo something
    }
}

2. Execute the Command:

var command = new GameCommand(paremeters);
invoker.Execute(command);

3. Undo the Command:

invoker.Undo();

4. Redo the Command:

invoker.Redo();

Note: invoker is a class that inherits from ICommandInvoker.


Testing Scene

Open CommandPatternDebug in the module's Debug/ folder and select Cube_GameTrackerAsComponent. Its Editor Test Tools block needs Play mode - the command stack is built in Awake - and reports Current position after every button.

What to do, in order:

  1. Execute a few move commands and watch the object step along.
  2. Undo them one at a time, back to the start.
  3. Redo them.
  4. Undo twice, then execute a new command, then try to redo.

Step 4 is the behaviour to internalise: executing a new command clears the redo stack. That is correct — the branch you had undone no longer exists — but it surprises people who expect redo to survive, and it is the difference between an undo history and a tree.


Technical Info

The following are the main scripts and workflow of the Keyboard System:

Interfaces: - ICommandBase: Interface that contains the methods Execute and Undo. - ICommandInvoker: Interface that contains the methods Execute, Undo, and Redo.

Classes&Scripts: - CommandInvoker: Basic implementation of ICommandInvoker. - Mn_CommandInvoker: MonoBehaviour wrapper of CommandInvoker.


© 2026 Marcello De Bonis. All rights reserved

Real dependenciesOpen in the map →

Depends on 1

Used by 0

  • No other module depends on it.

Measured from the repository: code references (asmdef) plus prefab and asset GUIDs. Importing this module alone brings in 1 module in total.