In object-oriented programming, the command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time. This information includes the method name, the object that owns the method and values for the method parameters.
Four terms always associated with the command pattern are command, receiver, invoker and client. A command object knows about receiver and invokes a method of the receiver. Values for parameters of the receiver method are stored in the command. The receiver then does the work. An invoker object knows how to execute a command, and optionally does bookkeeping about the command execution. The invoker does not know anything about a concrete command, it knows only about command interface. Both an invoker object and several command objects are held by a client object. The client decides which commands to execute at which points. To execute a command, it passes the command object to the invoker object.
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the class of the method or the method parameters. Using an invoker object allows bookkeeping about command executions to be conveniently performed, as well as implementing different modes for commands, which are managed by the invoker object, without the need for the client to be aware of the existence of bookkeeping or modes.
Command objects are useful for implementing
Action is a command object. In addition to the ability to perform the desired command, an Action may have an associated icon, keyboard shortcut, tooltip text, and so on. A toolbar button or menu item component may be completely initialized using only the Action object.addTask() method that adds a work item to an internal queue of tasks waiting to be done. It maintains a pool of threads that execute commands from the queue. The items in the queue are command objects. Typically these objects implement a common interface such as java.lang.Runnable that allows the thread pool to execute the command even though the thread pool class itself was written without any knowledge of the specific tasks for which it would be used.The terminology used to describe command pattern implementations is not consistent and can therefore be confusing. This is the result of ambiguity, the use of synonyms, and implementations that may obscure the original pattern by going well beyond it.
Consider a "simple" switch. In this example we configure the Switch with two commands: to turn the light on and to turn the light off.
A benefit of this particular implementation of the command pattern is that the switch can be used with any device, not just a light - the Switch in the following example turns a light on and off, but the Switch's constructor is able to accept any subclasses of Command for its two parameters. For example, you could configure the Switch to start an engine.
The following code is an implementation of Command pattern in C#.
namespace CommandPattern { using System; public interface ICommand { void Execute(); } /* The Invoker class */ public class Switch { ICommand _closedCommand; ICommand _openedCommand; public Switch(ICommand closedCommand, ICommand openedCommand) { this._closedCommand = closedCommand; this._openedCommand = openedCommand; } //close the circuit/power on public void Close() { this._closedCommand.Execute(); } //open the circuit/power off public void Open() { this._openedCommand.Execute(); } } /* An interface that defines actions that the receiver can perform */ public interface ISwitchable { void PowerOn(); void PowerOff(); } /* The Receiver class */ public class Light : ISwitchable { public void PowerOn() { Console.WriteLine("The light is on"); } public void PowerOff() { Console.WriteLine("The light is off"); } } /* The Command for turning on the device - ConcreteCommand #1 */ public class CloseSwitchCommand: ICommand { private ISwitchable _switchable; public CloseSwitchCommand(ISwitchable switchable) { _switchable = switchable; } public void Execute() { _switchable.PowerOn(); } } /* The Command for turning off the device - ConcreteCommand #2 */ public class OpenSwitchCommand : ICommand { private ISwitchable _switchable; public OpenSwitchCommand(ISwitchable switchable) { _switchable = switchable; } public void Execute() { _switchable.PowerOff(); } } /* The test class or client */ internal class Program { public static void Main(string[] args) { string arg = args.Length > 0 ? args[0].ToUpper() : null; ISwitchable lamp = new Light(); //Pass reference to the lamp instance to each command ICommand switchClose = new CloseSwitchCommand(lamp); ICommand switchOpen = new OpenSwitchCommand(lamp); //Pass reference to instances of the Command objects to the switch Switch @switch = new Switch(switchClose, switchOpen); if (arg == "ON") { // Switch (the Invoker) will invoke Execute() (the Command) on the command object - _closedCommand.Execute(); @switch.Close(); } else if (arg == "OFF") { //Switch (the Invoker) will invoke the Execute() (the Command) on the command object - _openedCommand.Execute(); @switch.Open(); } else { Console.WriteLine("Argument \"ON\" or \"OFF\" is required."); } } } }
import java.util.List; import java.util.ArrayList; /** The Command interface */ public interface Command { void execute(); } /** The Invoker class */ public class Switch { private List<Command> history = new ArrayList<Command>(); public void storeAndExecute(Command cmd) { this.history.add(cmd); // optional cmd.execute(); } } /** The Receiver class */ public class Light { public void turnOn() { System.out.println("The light is on"); } public void turnOff() { System.out.println("The light is off"); } } /** The Command for turning on the light - ConcreteCommand #1 */ public class FlipUpCommand implements Command { private Light theLight; public FlipUpCommand(Light light) { this.theLight = light; } @Override // Command public void execute() { theLight.turnOn(); } } /** The Command for turning off the light - ConcreteCommand #2 */ public class FlipDownCommand implements Command { private Light theLight; public FlipDownCommand(Light light) { this.theLight = light; } @Override // Command public void execute() { theLight.turnOff(); } } /* The test class or client */ public class PressSwitch { public static void main(String[] args){ // Check number of arguments if (args.length != 1) { System.err.println("Argument \"ON\" or \"OFF\" is required."); System.exit(-1); } Light lamp = new Light(); Command switchUp = new FlipUpCommand(lamp); Command switchDown = new FlipDownCommand(lamp); Switch mySwitch = new Switch(); switch(args[0]) { case "ON": mySwitch.storeAndExecute(switchUp); break; case "OFF": mySwitch.storeAndExecute(switchDown); break; default: System.err.println("Argument \"ON\" or \"OFF\" is required."); System.exit(-1); } } }
Using a functional interface.
/** * The Command functional interface.<br/> */ @FunctionalInterface public interface Command { public void apply(); } /** * The CommandFactory class.<br/> */ import java.util.HashMap; import java.util.stream.Collectors; public final class CommandFactory { private final HashMap<String, Command> commands; private CommandFactory() { commands = new HashMap<>(); } public void addCommand(String name, Command command) { commands.put(name, command); } public void executeCommand(String name) { if (commands.containsKey(name)) { commands.get(name).apply(); } } public void listCommands() { System.out.println("Enabled commands: " + commands.keySet().stream().collect(Collectors.joining(", "))); } /* Factory pattern */ public static CommandFactory init() { CommandFactory cf = new CommandFactory(); // commands are added here using lambdas. It is also possible to dynamically add commands without editing the code. cf.addCommand("Light on", () -> System.out.println("Light turned on")); cf.addCommand("Light off", () -> System.out.println("Light turned off")); return cf; } } public final class Main { public static void main(String[] args) { CommandFactory cf = CommandFactory.init(); cf.executeCommand("Light on"); cf.listCommands(); } }
The following code is an implementation of Command pattern in Python.
class Switch(object): """The INVOKER class""" def __init__(self): self._history = () @property def history(self): return self._history def execute(self, command): self._history = self._history + (command,) command.execute() class Command(object): """The COMMAND interface""" def __init__(self, obj): self._obj = obj def execute(self): raise NotImplementedError class TurnOnCommand(Command): """The COMMAND for turning on the light""" def execute(self): self._obj.turn_on() class TurnOffCommand(Command): """The COMMAND for turning off the light""" def execute(self): self._obj.turn_off() class Light(object): """The RECEIVER class""" def turn_on(self): print("The light is on") def turn_off(self): print("The light is off") class LightSwitchClient(object): """The CLIENT class""" def __init__(self): self._lamp = Light() self._switch = Switch() @property def switch(self): return self._switch def press(self, cmd): cmd = cmd.strip().upper() if cmd == "ON": self._switch.execute(TurnOnCommand(self._lamp)) elif cmd == "OFF": self._switch.execute(TurnOffCommand(self._lamp)) else: print("Argument 'ON' or 'OFF' is required.") # Execute if this file is run as a script and not imported as a module if __name__ == "__main__": light_switch = LightSwitchClient() print("Switch ON test.") light_switch.press("ON") print("Switch OFF test.") light_switch.press("OFF") print("Invalid Command test.") light_switch.press("****") print("Command history:") print(light_switch.switch.history)
# Invoker class Switch attr_reader :history def execute(cmd) @history ||= [] @history << cmd.execute end end # Command Interface class Command attr_reader :obj def initialize(obj) @obj = obj end def execute raise NotImplementedError end end # Command for turning on class TurnOnCommand < Command def execute obj.turn_on end end # Command for turning off class TurnOffCommand < Command def execute obj.turn_off end end # Receiver class Light def turn_on 'the light is on' end def turn_off 'the light is off' end end # Client class LightSwitchClient attr_reader :switch def initialize @lamp = Light.new @switch = Switch.new end def switch_for(cmd) case cmd when 'on' then @switch.execute(TurnOnCommand.new(@lamp)) when 'off' then @switch.execute(TurnOffCommand.new(@lamp)) else puts 'Sorry, I so sorry' end end end client = LightSwitchClient.new client.switch_for('on') client.switch_for('off') client.switch.history #=> ['the light is on', 'the light is off']
/* The Command interface */ trait Command { def execute() } /* The Invoker class */ class Switch { private var history: List[Command] = Nil def storeAndExecute(cmd: Command) { cmd.execute() this.history :+= cmd } } /* The Receiver class */ class Light { def turnOn() = println("The light is on") def turnOff() = println("The light is off") } /* The Command for turning on the light - ConcreteCommand #1 */ class FlipUpCommand(theLight: Light) extends Command { def execute() = theLight.turnOn() } /* The Command for turning off the light - ConcreteCommand #2 */ class FlipDownCommand(theLight: Light) extends Command { def execute() = theLight.turnOff() } /* The test class or client */ object PressSwitch { def main(args: Array[String]) { val lamp = new Light() val switchUp = new FlipUpCommand(lamp) val switchDown = new FlipDownCommand(lamp) val s = new Switch() try { args(0).toUpperCase match { case "ON" => s.storeAndExecute(switchUp) case "OFF" => s.storeAndExecute(switchDown) case _ => println("Argument \"ON\" or \"OFF\" is required.") } } catch { case e: Exception => println("Arguments required.") } } }
The following code is an implementation of Command pattern in JavaScript.
/* The Invoker function */ "use strict"; class Switch { constructor() { this._commands = []; } storeAndExecute(command) { this._commands.push(command); command.execute(); } } class Light { turnOn() { console.log('turn on') } turnOff() { console.log('turn off') } } class FlipDownCommand { constructor(light) { this._light = light; } execute() { this._light.turnOff(); } } class FlipUpCommand { constructor(light) { this._light = light; } execute() { this._light.turnOn(); } } var light = new Light(); var switchUp = new FlipUpCommand(light); var switchDown = new FlipDownCommand(light); var s = new Switch(); s.storeAndExecute(switchUp); s.storeAndExecute(switchDown);
The following code is an implementation of Command pattern in Coffeescript
# The Invoker function class Switch _commands = [] storeAndExecute: (command) -> _commands.push(command) command.execute() # The Receiver function class Light turnOn: -> console.log ('turn on') turnOff: -> console.log ('turn off') # The Command for turning on the light - ConcreteCommand #1 class FlipUpCommand constructor: (@light) -> execute: -> @light.turnOn() # The Command for turning off the light - ConcreteCommand #2 class FlipDownCommand constructor: (@light) -> execute: -> @light.turnOff() light = new Light() switchUp = new FlipUpCommand(light) switchDown = new FlipDownCommand(light) s = new Switch() s.storeAndExecute(switchUp) s.storeAndExecute(switchDown)