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

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

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

    人在江湖

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      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 人在江湖 閱讀(1565) 評論(0)  編輯  收藏 所屬分類: design pattern
    主站蜘蛛池模板: 中文字幕无码免费久久9一区9 | 免费在线观看一级片| 免费无码又爽又刺激网站| 麻豆国产精品免费视频| 免费看又爽又黄禁片视频1000| 日韩中文无码有码免费视频 | 国产成人3p视频免费观看| 亚洲国产一级在线观看| 久久久久亚洲爆乳少妇无| 亚洲无删减国产精品一区| 亚洲一卡二卡三卡四卡无卡麻豆| 亚洲精品国产日韩| 国产免费伦精品一区二区三区 | 亚洲综合色婷婷在线观看| 国产在线精品一区免费香蕉| 四虎影视www四虎免费| 亚洲AV无码日韩AV无码导航| 77777亚洲午夜久久多喷| 久久青草91免费观看| 国产免费资源高清小视频在线观看| 久久亚洲精品成人777大小说| 美女露100%胸无遮挡免费观看| 最新国产乱人伦偷精品免费网站 | 亚洲AV日韩AV鸥美在线观看| 99久久免费观看| 久久久久亚洲AV无码专区桃色| 久久免费99精品国产自在现线| 久久精品国产96精品亚洲| 亚洲网站免费观看| 亚洲AV人无码激艳猛片| 100000免费啪啪18免进| 亚洲国产成人片在线观看无码| 四虎影视在线看免费观看| 无码人妻一区二区三区免费| 精品亚洲综合在线第一区| 黄网站色在线视频免费观看| 国产精品亚洲av色欲三区| 日韩国产免费一区二区三区| 自怕偷自怕亚洲精品| 91国内免费在线视频| 亚洲精品中文字幕无乱码麻豆|