<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    人在江湖

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      82 Posts :: 10 Stories :: 169 Comments :: 0 Trackbacks

    參考 http://en.wikipedia.org/wiki/Command_pattern

    Command Design Pattern Class Diagram.png

    /*the Invoker class*/
    public class Switch {
     
        private Command flipUpCommand;
        private Command flipDownCommand;
     
        public Switch(Command flipUpCmd, Command flipDownCmd) {
             this.flipUpCommand = flipUpCmd;
             this.flipDownCommand = flipDownCmd;
        }
     
        public void flipUp() {
             flipUpCommand.execute();
        }
     
        public void flipDown() {
             flipDownCommand.execute();
        }
    }
     
    /*Receiver class*/
    public class Light {
     
         public Light() {  }
     
         public void turnOn() {
            System.out.println("The light is on");
         }
     
         public void turnOff() {
            System.out.println("The light is off");
         }
    }
     
    /*the Command interface*/
    public interface Command {
        void execute();
    }
     
    /*the Command for turning on the light*/
    public class FlipUpCommand implements Command {
     
       private Light theLight;
     
       public FlipUpCommand(Light light) {
            this.theLight=light;
       }
     
       public void execute(){
          theLight.turnOn();
       }
    }
     
    /*the Command for turning off the light*/
    public class FlipDownCommand implements Command {
     
       private Light theLight;
     
       public FlipDownCommand(Light light) {
            this.theLight=light;
       }
     
       public void execute() {
          theLight.turnOff();
       }
    }
     
    /*The test class or client*/
    public class PressSwitch {
     
       public static void main(String[] args) {
           Light lamp = new Light();
           Command switchUp = new FlipUpCommand(lamp);
           Command switchDown = new FlipDownCommand(lamp);
     
           // See criticism of this model above:
           // The switch itself should not be aware of lamp details (switchUp, switchDown) 
           // either directly or indirectly
           Switch s = new Switch(switchUp,switchDown);
     
           try {
               if (args[0].equalsIgnoreCase("ON")) {
                    s.flipUp();
               } else if (args[0].equalsIgnoreCase("OFF")) {
                   s.flipDown();
               } else {
                   System.out.println("Argument \"ON\" or \"OFF\" is required.");
               }
           } catch (Exception e){
               System.out.println("Arguments required.");
           }
       }
    }
    =====================

    java tip (http://www.javaworld.com/javaworld/javatips/jw-javatip68.html?page=3) 給了類似的例子,多了Fan作為receiver:

    TestCommand.java
    class Fan {
            public void startRotate() {
                    System.out.println("Fan is rotating");
            }
            public void stopRotate() {
                    System.out.println("Fan is not rotating");
            }
    }
    class Light {
            public void turnOn( ) {
                    System.out.println("Light is on ");
            }
            public void turnOff( ) {
                    System.out.println("Light is off");
            }
    }
    class Switch {
            private Command UpCommand, DownCommand;
            public Switch( Command Up, Command Down) {
                    UpCommand = Up; // concrete Command registers itself with the invoker
                    DownCommand = Down;
            }
            void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver
                            UpCommand . execute ( ) ;                          
            }
            void flipDown( ) {
                            DownCommand . execute ( );
            }
    }
    class LightOnCommand implements Command {
            private Light myLight;
            public LightOnCommand ( Light L) {
                    myLight  =  L;
            }
            public void execute( ) {
                    myLight . turnOn( );
            }
    }
    class LightOffCommand implements Command {
            private Light myLight;
            public LightOffCommand ( Light L) {
                    myLight  =  L;
            }
            public void execute( ) {
                    myLight . turnOff( );
            }
    }
    class FanOnCommand implements Command {
            private Fan myFan;
            public FanOnCommand ( Fan F) {
                    myFan  =  F;
            }
            public void execute( ) {
                    myFan . startRotate( );
            }
    }
    class FanOffCommand implements Command {
            private Fan myFan;
            public FanOffCommand ( Fan F) {
                    myFan  =  F;
            }
            public void execute( ) {
                    myFan . stopRotate( );
            }
    }
    public class TestCommand {
                    public static void main(String[] args) {
                            Light  testLight = new Light( );
                            LightOnCommand testLOC = new LightOnCommand(testLight);
                            LightOffCommand testLFC = new LightOffCommand(testLight);
                            Switch testSwitch = new Switch( testLOC,testLFC);      
                            testSwitch.flipUp( );
                            testSwitch.flipDown( );
                            Fan testFan = new Fan( );
                            FanOnCommand foc = new FanOnCommand(testFan);
                            FanOffCommand ffc = new FanOffCommand(testFan);
                            Switch ts = new Switch( foc,ffc);
                            ts.flipUp( );
                            ts.flipDown( );
                    }
    }              
    Command.java
    public interface Command {
            public abstract void execute ( );
    }

    posted on 2011-02-12 23:25 人在江湖 閱讀(1557) 評論(0)  編輯  收藏 所屬分類: design pattern
    主站蜘蛛池模板: 成全动漫视频在线观看免费高清版下载| 亚洲中文字幕一区精品自拍| 91视频精品全国免费观看| 亚洲av午夜精品一区二区三区| 国产精品亚洲综合天堂夜夜| 日韩一品在线播放视频一品免费| 亚洲色中文字幕在线播放| 国内自产拍自a免费毛片| 亚洲AV成人一区二区三区观看| 国产精品免费一级在线观看| 青草青草视频2免费观看| 伊人久久亚洲综合影院| 中文字幕在线视频免费观看| 国产亚洲一区二区在线观看| 久久久免费的精品| 亚洲免费人成视频观看| 日韩免费视频网站| 一级全免费视频播放| 香蕉视频在线观看亚洲| 免费影院未满十八勿进网站| 国产成人人综合亚洲欧美丁香花 | 亚洲高清无在码在线无弹窗| 在线永久看片免费的视频| 亚洲乱色伦图片区小说| 免费国产综合视频在线看| a毛片在线看片免费| 亚洲日韩乱码中文无码蜜桃 | 久久被窝电影亚洲爽爽爽| 50岁老女人的毛片免费观看| 亚洲码和欧洲码一码二码三码| 国产亚洲精品福利在线无卡一| 99免费视频观看| 亚洲爆乳成av人在线视菜奈实 | 亚洲国产成AV人天堂无码| 日韩免费福利视频| 国产色爽免费无码视频| 亚洲伊人久久大香线焦| 亚洲A丁香五香天堂网| 小草在线看片免费人成视久网| 亚洲av色香蕉一区二区三区| 亚洲成AV人片在线观看无|