<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 人在江湖 閱讀(1559) 評論(0)  編輯  收藏 所屬分類: design pattern
    主站蜘蛛池模板: 亚洲自国产拍揄拍| 57PAO成人国产永久免费视频 | 免费人成激情视频在线观看冫| 一本色道久久综合亚洲精品蜜桃冫 | 亚洲精品亚洲人成人网| 日韩免费高清视频网站| h在线观看视频免费网站| 在线人成免费视频69国产| 日本特黄特色AAA大片免费| 亚洲另类无码专区丝袜| 亚洲图片中文字幕| 精品亚洲成a人片在线观看| 亚洲日韩中文字幕在线播放| 亚洲国产成人五月综合网| 成**人免费一级毛片| 嫖丰满老熟妇AAAA片免费看| 99爱视频99爱在线观看免费| 国产在线精品观看免费观看| 亚洲免费无码在线| 色爽黄1000部免费软件下载| 国产亚洲综合视频| 欧美激情综合亚洲一二区| 亚洲国产精品ⅴa在线观看| 亚洲国产视频久久| 色老板亚洲视频免在线观| jlzzjlzz亚洲jzjzjz| 亚洲一级毛片免费观看| 久久精品国产亚洲av麻豆蜜芽| 亚洲国产成人综合| 亚洲AV成人噜噜无码网站| 亚洲AV无码乱码麻豆精品国产| 色偷偷女男人的天堂亚洲网| 亚洲狠狠成人综合网| 亚洲色无码专区一区| 国产亚洲精品AAAA片APP| 国产亚洲视频在线播放大全| 午夜免费国产体验区免费的| 国产精品玖玖美女张开腿让男人桶爽免费看| 一级毛片a免费播放王色| 国产精品免费久久久久影院| 91精品成人免费国产|