Command

Gamma et al. 95

Intent

  • Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

Motivation

  • Sometimes it’s necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.

  • For example, graphical user interface buttons: only clients that create and use the button can implement the actions linked to this button.

Example

command example

Solution

  • Implement requests as objects, whose classes are subclasses of the “Command” abstract class, which defines an interface for operation execution.

Structure

command structure

Implementation Tradeoffs

  • Commands store state information for undo and redo operations.

  • Commands may be composed (see Composite pattern).

  • Sometimes, commands may implement operations directly, without delegation.

Author and Date

  • Design Patterns: Elements of Reusable Object-Oriented Software. Erich Gamma, Richard Helm,Ralph Johnson, and John Vlissides. Addison Wesley. October 1994.

More Examples

java.lang.Runnable

cd runnable

More Implementation Tradeoffs

Command in Java 8 with Lambdas

Lambdas

  • Lambdas are new in Java 8 (but implemented in many languages)

  • Anonymous function (unnamed)

  • format: (arguments) -> (body)

  • Improve code conciseness and permit to handle operations as objects

Examples:
(int a, int b) -> {  return a + b; }

(a, b) -> {
  System.out.println(a + b);
  return a + b;
}

() -> System.out.println("Hello World");

Java Collection Frameworks Example

List<String> tags = new ArrayList<>();

// The normal syntax of the forEach lambda
tags.forEach((String each) -> {System.out.println(each);});

// Sugar syntax to shorten the lambda
tags.forEach(each -> System.out.println(each));
//or
tags.forEach(System.out::println)

The Macro class

public class Macro {
 private final List<Command> cmds;

 public Macro() {
  cmds = new ArrayList<>();
 }

 public void addCommand(Command cmd) {
  System.out.println(cmd);
  cmds.add(cmd);
}

 public void run() {
  cmds.forEach(Command::execute);
 }
}

Adding commands to a macro

Macro macro = new Macro();
Editor editor = new EditorImpl();

// Java 7 version: anonymous classes.
macro.addCommand(new Command() {
 @Override public void execute() {
  editor.open();
}});

// Java 8 version: lambdas
macro.addCommand(() -> editor.open());
// or:
macro.addCommand(editor::open);

macro.run();

Back to main presentation