Alias
Action/Transcation
Command pattern
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 object to execute these methods is also stored in the command object by aggregation. The receiver then does the work when the execute() method in command is called. 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 the command interface. Invoker object(s), command objects and receiver objects are held by a client object, the client decides which receiver objects it assigns to the command objects, and which commands it assigns to the invoker. 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.
The central ideas of this design pattern closely mirror the semantics of first-class functions and higher-order functions in functional programming languages. Specifically, the invoker object is a higher-order function of which the command object is a first-class argument. --Wikipedia
巴拉巴拉一堆大概讲的就是封装命令。
Structure
Example
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
Content c = new Content();
Command insertCommand = new InsertCommand(c);
insertCommand.doit();
insertCommand.undo();
Command copyCommand = new CopyCommand(c);
copyCommand.doit();
copyCommand.undo();
Command deleteCommand = new DeleteCommand(c);
deleteCommand.doit();
deleteCommand.undo();
List<Command> commands = new ArrayList<>();
commands.add(new InsertCommand(c));
commands.add(new CopyCommand(c));
commands.add(new DeleteCommand(c));
for(Command comm : commands) {
comm.doit();
}
System.out.println(c.msg);
for(int i= commands.size()-1; i>=0; i--) {
commands.get(i).undo();
}
System.out.println(c.msg);
}
}
abstract class Command {
public abstract void doit(); //exec run
public abstract void undo();
}
class Content {
String msg = "hello everybody ";
}
class CopyCommand extends Command {
Content c;
public CopyCommand(Content c) {
this.c = c;
}
@Override
public void doit() {
c.msg = c.msg + c.msg;
System.out.println("copy:"+c.msg);
}
@Override
public void undo() {
c.msg = c.msg.substring(0, c.msg.length()/2);
System.out.println("copy undo:"+c.msg);
}
}
class DeleteCommand extends Command {
Content c;
String deleted;
public DeleteCommand(Content c) {
this.c = c;
}
@Override
public void doit() {
deleted = c.msg.substring(0, 5);
c.msg = c.msg.substring(5);
System.out.println("delete:"+c.msg);
}
@Override
public void undo() {
c.msg = deleted + c.msg;
System.out.println("delete undo:"+c.msg);
}
}
class InsertCommand extends Command {
Content c;
String strToInsert = "abcabc123123";
public InsertCommand(Content c) {
this.c = c;
}
@Override
public void doit() {
c.msg = c.msg + strToInsert;
System.out.println("insert:"+c.msg);
}
@Override
public void undo() {
c.msg = c.msg.substring(0, c.msg.length()-strToInsert.length());
System.out.println("insert undo:"+c.msg);
}
}
Summary
- 封装命令
- Command模式和责任链模式结合可以实现撤销操作