文檔查閱說明:
定義:將一個請求封裝成一個對象,從而讓你使用不同的請求把客戶端參數化,對請求排隊或者記錄請求日誌,可以提供命令的撤銷和恢復功能
使用場景:(認爲是命令的地方就可以採用命令模式)
● 在GUI開發中,一個按鈕的點擊是一個命令;
● 模擬DOS命令的時候;
● 觸發-反饋機制的處理
public class Recevier {
public void action() {
System.out.println("執行請求");
}
}
public abstract class Command {
private Recevier recevier;
public Recevier getRecevier() {
return recevier;
}
public void setRecevier(Recevier recevier) {
this.recevier = recevier;
}
public abstract void execute();
}
public class ConCreateCommand extends Command {
public ConCreateCommand(Recevier recevier) {
this.setRecevier(recevier);
}
@Override
public void execute() {
this.getRecevier().action();
}
}
public class Invoke {
private Command command;
public Command getCommand() {
return command;
}
public void setCommand(Command command) {
this.command = command;
}
public void executeCommand() {
command.execute();
}
}
public class Test {
public static void main(String[] args) {
Command command = new ConCreateCommand(new Recevier());
Invoke invoke = new Invoke();
invoke.setCommand(command);
invoke.executeCommand();
}
}