一天学习一个设计模式之命令模式
生活随笔
收集整理的這篇文章主要介紹了
一天学习一个设计模式之命令模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
命令模式(Command),將一個請求封裝為一個對象,從而使你可用不同的請求對客戶進行參數化;對請求排隊或記錄請求日志,以及支持可撤銷的操作。
?
代碼如下:
1 /** 2 * 抽象命令角色類 3 */ 4 public interface Command { 5 /** 6 * 執行方法 7 */ 8 void execute(); 9 } 1 /** 2 * 接收者角色類 3 */ 4 public class Receiver { 5 /** 6 * 真正執行命令相應的操作 7 */ 8 public void action(){ 9 System.out.println("執行操作"); 10 } 11 } 1 /** 2 * 具體命令角色類 3 */ 4 public class ConcreteCommand implements Command { 5 6 //持有相應的接收者對象 7 private Receiver receiver = null; 8 9 /** 10 * 構造方法 11 * 12 * @param receiver 13 */ 14 public ConcreteCommand(Receiver receiver) { 15 this.receiver = receiver; 16 } 17 18 19 @Override 20 public void execute() { 21 //通常會轉調用接收者對象的相應方法,讓接收者來真正執行功能 22 receiver.action(); 23 } 24 } 1 /** 2 * 請求者角色類 3 */ 4 public class Invoker { 5 /** 6 * 持有命令對象 7 */ 8 private Command command=null; 9 10 /** 11 * 構造方法 12 * @param command 13 */ 14 public Invoker(Command command) { 15 this.command = command; 16 } 17 18 /** 19 * 行動方法 20 */ 21 public void action(){ 22 command.execute(); 23 } 24 } 1 /** 2 * 客戶端 3 */ 4 public class Client { 5 public static void main(String[] args) { 6 //創建接收者 7 Receiver receiver=new Receiver(); 8 //創建命令對象,設定它的接收者 9 Command command=new ConcreteCommand(receiver); 10 //創建請求者,把命令對象設置進去 11 Invoker invoker=new Invoker(command); 12 //執行方法 13 invoker.action(); 14 } 15 }?
以下例子是《大話設計模式》中的例子:
1 /** 2 * 烤串者(命令執行者) 3 */ 4 public class Barbecuer { 5 public void bakeMutton(){ 6 System.out.println("烤羊肉串"); 7 } 8 public void bakeChickenWing(){ 9 System.out.println("烤雞翅"); 10 } 11 } 1 /** 2 * 抽象命令類 3 */ 4 public abstract class Command { 5 //抽象命令類,只需要確定‘烤肉串者'是誰 6 protected Barbecuer receiver; 7 8 /** 9 * 10 * @param receiver 參數為命名執行人 11 */ 12 public Command(Barbecuer receiver) { 13 this.receiver = receiver; 14 } 15 16 //執行命令 17 abstract public void excuteCommand(); 18 } 1 /** 2 * 烤雞翅命令 3 */ 4 public class BakeChickenWingCommand extends Command { 5 public BakeChickenWingCommand(Barbecuer receiver) { 6 super(receiver); 7 } 8 9 @Override 10 public void excuteCommand() { 11 receiver.bakeChickenWing(); 12 } 13 } 1 /** 2 * 烤羊肉串命令 3 */ 4 public class BakeMuttonCommand extends Command { 5 6 public BakeMuttonCommand(Barbecuer receiver) { 7 super(receiver); 8 } 9 10 @Override 11 public void excuteCommand() { 12 receiver.bakeMutton(); 13 } 14 } 1 /** 2 * 服務員類 3 */ 4 public class Waiter { 5 private List<Command> orders=new ArrayList<>(); 6 /** 7 * 不管用戶想要什么烤肉,反正都是命令,只管記錄訂單,然后通知烤肉者執行 8 * @param command 9 */ 10 public void setOrder(Command command){ 11 orders.add(command); 12 System.out.println("增加訂單:"+command.toString()+" 時間:"+new Date()); 13 } 14 public void cancelOrder(Command command){ 15 orders.remove(command); 16 System.out.println("取消訂單:"+command.toString()+" 時間:"+new Date()); 17 } 18 //通知執行 19 public void notifyExcuteCommand(){ 20 for (Command command:orders 21 ) { 22 command.excuteCommand(); 23 } 24 25 } 26 } 1 public class Client { 2 public static void main(String[] args) { 3 //命令執行人 4 Barbecuer boy=new Barbecuer(); 5 Command bakeMuttonCommand1=new BakeMuttonCommand(boy); 6 Command bakeMuttonCommand2=new BakeMuttonCommand(boy); 7 Command bakeChickenWingCommand1=new BakeChickenWingCommand(boy); 8 //命令布人 9 Waiter girl=new Waiter(); 10 11 //開門營業 12 //記錄訂單命令 13 girl.setOrder(bakeMuttonCommand1); 14 girl.setOrder(bakeMuttonCommand2); 15 girl.setOrder(bakeChickenWingCommand1); 16 17 //點菜完畢,通知廚房(下達命令) 18 girl.notifyExcuteCommand(); 19 girl.cancelOrder(bakeChickenWingCommand1); 20 } 21 }?命令模式作用
第一,它能較容易地設計一個命令隊列;
第二,在需要的情況下,可以較容易地將命令記入日志;
第三,允許接收請求的一方決定是否要否決請求。
第四,可以容易地實現對請求的撤銷和重做。
第五,由于加新的具體命令類不影響其他類,因此增加新的具體命令類很容易。
命令模式把請求一個操作的對象與知道怎么執行一個操作的對象分割開。
轉載于:https://www.cnblogs.com/gousheng107/p/8126814.html
總結
以上是生活随笔為你收集整理的一天学习一个设计模式之命令模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c# 后台 添加datable 数据
- 下一篇: HDU 5869.Different G