源自
Command模式
一
將一個請求封裝為一個對象
二
Commad模式是一種對象行為模式
三
例子
view plaincopy to clipboardprint?
public class Document {
public void display() {
System
}
public void undo() {
System
}
public void redo() {
System
}
}
public interface DocumentCommand {
public void execute();
}
public class DisplayCommand implements DocumentCommand {
private Document document;
public DisplayCommand(Document doc) {
document = doc;
}
public void execute() {
document
}
}
public class RedoCommand implements DocumentCommand {
private Document document;
public RedoCommand(Document doc) {
document = doc;
}
public void execute() {
document
}
}
public class UndoCommand implements DocumentCommand {
private Document document;
public UndoCommand(Document doc) {
document = doc;
}
public void execute() {
document
}
}
public class DocumentInvoker {
private DisplayCommand _dcmd;
private UndoCommand _ucmd;
private RedoCommand _rcmd;
public DocumentInvoker(DisplayCommand dcmd
RedoCommand rcmd) {
this
this
this
}
public void display() {
_dcmd
}
public void undo() {
_ucmd
}
public void redo() {
_rcmd
}
}
public class CommandTest {
public static void main(String[] args) {
Document doc = new Document();
DisplayCommand display = new DisplayCommand(doc);
UndoCommand undo = new UndoCommand(doc);
RedoCommand redo = new RedoCommand(doc);
DocumentInvoker invoker = new DocumentInvoker(display
invoker
invoker
invoker
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25766.html