silvermyth

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            1 隨筆 :: 12 文章 :: 1 評(píng)論 :: 0 Trackbacks
          命令模式是一種應(yīng)用比較廣泛的模式,它的最核心思想是命令的接受者和執(zhí)行者獨(dú)立,從而大大降低了代碼的耦合度。舉個(gè)例子,我家小朋友晚上睡覺(jué)前對(duì)媽媽(命令接受者)說(shuō)第二天早上要吃蒸雞蛋羹(發(fā)送命令);對(duì)于小朋友來(lái)說(shuō)最重要的就是第二天早上一醒來(lái)看到桌上有一碗雞蛋羹,而她根本不關(guān)心這是誰(shuí)做的(誰(shuí)執(zhí)行的相關(guān)命令),媽媽親自做的也好,爸爸愛(ài)心奉獻(xiàn)的也好,飯店里買的也不是不可以。
          在適配器模式中我們提到,Garin新交了個(gè)女朋友,他每天都會(huì)收到如下命令:
          1 public interface Command {
          2     public void execute();
          3 }
          其中一部分是做飯,一部分是洗衣服;Garin必須親自完成嗎,不一定:
           1 public class CookCommand implements GirlFriendCommand {
           2 
           3     private String mealName = null;
           4     
           5     public CookCommand(String mealName) {
           6         super();
           7         this.mealName = mealName;
           8     }
           9 
          10     private MealSupplier mealSupplier = null;
          11     
          12     public void setMealSupplier(MealSupplier mealSupplier) {
          13         this.mealSupplier = mealSupplier;
          14     }
          15 
          16     @Override
          17     public void execute() {
          18         mealSupplier.cook(mealName);
          19     }
          20 
          21 }
          真正完成做飯的是一個(gè)抽象的MealSupplier.
          1 public interface MealSupplier {
          2     public void cook(String name);
          3 }
          它可能代表的是一個(gè)飯店:
           1 public class Restaurant implements MealSupplier{
           2     
           3     public Restaurant() {
           4         super();
           5     }
           6 
           7     @Override
           8     public void cook(String name) {
           9         System.out.println("飯店燒好了"+name);
          10     }
          11     
          12 }
          同樣的,真正洗衣服的是抽象的CanWash.
           1 public class WashCommand implements GirlFriendCommand {
           2 
           3     private CanWash wash = null;
           4     
           5     public void setWash(CanWash wash) {
           6         this.wash = wash;
           7     }
           8 
           9     @Override
          10     public void execute() {
          11         if(wash != null){
          12             wash.washClothes();
          13         }
          14     }
          15 
          16 }
          它代表的可能是洗衣機(jī):
          1 public interface CanWash {
          2     public void washClothes();
          3 }

          1 public class WashingMachine implements CanWash {
          2 
          3     @Override
          4     public void washClothes() {
          5         System.out.println("洗衣機(jī)洗好了衣服");
          6     }
          7 
          8 }
          Garin要做的就是坐等命令,如何執(zhí)行完全不管:
           1 public class GirlFriendCommandTest {
           2 
           3     /**
           4      * @param args
           5      */
           6     public static void main(String[] args) {
           7         BoyFriend bf = new BoyFriend("Garin");
           8         
           9         bf.addCommand(new CookCommand("魚(yú)香肉絲"));
          10         bf.addCommand(new WashCommand());
          11         bf.action();
          12     }
          13 
          14 }
          執(zhí)行完命令后效果如下:
          Garin開(kāi)始執(zhí)行命令
          飯店燒好了魚(yú)香肉絲
          洗衣機(jī)洗好了衣服
          posted on 2015-07-28 21:22 Gavin Li 閱讀(232) 評(píng)論(1)  編輯  收藏 所屬分類: 設(shè)計(jì)模式

          評(píng)論

          # re: 閑話Java設(shè)計(jì)模式三:命令模式 2015-08-04 08:58 Philip
          BoyFriend怎么沒(méi)有定義啊? 不好意思  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 塔河县| 界首市| 沧州市| 额济纳旗| 安吉县| 哈密市| 二连浩特市| 乡城县| 边坝县| 陵水| 白山市| 正镶白旗| 依兰县| 呼伦贝尔市| 江山市| 宝清县| 阿克陶县| 酒泉市| 封开县| 赤壁市| 敦化市| 保亭| 星座| 甘孜县| 宿迁市| 苗栗市| 潢川县| 大冶市| 大兴区| 邛崃市| 大新县| 清徐县| 潢川县| 礼泉县| 梨树县| 古田县| 九龙县| 米易县| 凯里市| 松潘县| 陇南市|