(int a, int b) -> { return a + b; }
(a, b) -> {
System.out.println(a + b);
return a + b;
}
() -> System.out.println("Hello World");
Gamma et al. 95
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
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.
Implement requests as objects, whose classes are subclasses of the “Command” abstract class, which defines an interface for operation execution.
Commands store state information for undo and redo operations.
Commands may be composed (see Composite pattern).
Sometimes, commands may implement operations directly, without delegation.
Design Patterns: Elements of Reusable Object-Oriented Software. Erich Gamma, Richard Helm,Ralph Johnson, and John Vlissides. Addison Wesley. October 1994.
Command in Java 8 with 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
(int a, int b) -> { return a + b; }
(a, b) -> {
System.out.println(a + b);
return a + b;
}
() -> System.out.println("Hello World");
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)
Macro
classpublic 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);
}
}
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();