Java設計モード-コマンドモード(Command Pattern)


コマンドモード(Command Pattern)は、オブジェクトの動作モードであり、リクエストをオブジェクトの内部にカプセル化することを意図している.
 コマンド・モードの役割は次のとおりです.
  • クライアント(Client)ロール:特定のコマンドを作成し、コマンドの受信者
  • を指定します.
  • コマンド(Command) 役割: コマンド共通の操作インタフェース
  • を定義する
  • 特定のコマンド(Concrete Command)ロール:コマンドの受信者と動作の結合を定義します.コマンドインタフェースの共通インタフェースを実現し、受信者の対応する操作を呼び出す.
  • リクエスト者(Invoker)ロール:コマンドオブジェクトを呼び出してリクエストを実行します.
  • 受信者(Receiver)ロール:リクエスト
  • の実装と実行を担当します.
       例:
             生活の中でテレビだけが良いコマンドモードの例です. リモコンはInvokerです テレビはReceiverです.リモコンを使う人はClientです.リモコンのボタンはConcrete Commandです.
            仮にテレビが オン、オフ、チャンネル切り替えの基本機能があります.リモコンには、オン、オフボタン、チャンネル1、チャンネル2、チャンネル3があります. で行ないます. まずリモコンとテレビをマッチングし、マッチングしたらリモコンでテレビを制御する必要があります.
     
    /**
     *        ,             
     * @author zhangwei_david
     * @version $Id: TV.java, v 0.1 2014 11 21    4:34:05 zhangwei_david Exp $
     */
    public interface TV {
    
        /**
         *      
         */
        public void open();
    
        /**
         *      
         */
        public void close();
    
        /**
         *     
         *
         * @param channel
         */
        public void change(int channel);
    }
    

     
    /**
     *      
     * @author zhangwei_david
     * @version $Id: SonyTV.java, v 0.1 2014 11 21    4:35:05 zhangwei_david Exp $
     */
    public class SonyTV implements TV {
    
        private enum State {
            OPEN, CLOSE;
        }
    
        private State state = State.CLOSE;
    
        /**
         * @see com.cathy.demo.pattern.command.TV#open()
         */
        public void open() {
            if (state == State.CLOSE) {
                state = State.OPEN;
                System.out.println("     ");
            }
        }
    
        /**
         * @see com.cathy.demo.pattern.command.TV#close()
         */
        public void close() {
            if (state == State.OPEN) {
                state = State.CLOSE;
                System.out.println("     ");
            }
        }
    
        /**
         * @see com.cathy.demo.pattern.command.TV#change(int)
         */
        public void change(int channel) {
            if (state == State.OPEN) {
                System.out.println("    " + channel + "   ");
            }
        }
    
    }
    

     
    /**
     *       
     * @author zhangwei_david
     * @version $Id: Command.java, v 0.1 2014 11 21    4:27:15 zhangwei_david Exp $
     */
    public interface Command {
        /**
         *     
         */
        public void execute();
    }
    

     
    /**
     *     -  
     * @author zhangwei_david
     * @version $Id: Open.java, v 0.1 2014 11 21    4:28:37 zhangwei_david Exp $
     */
    public class Open implements Command {
    
        /**   **/
        private TV tv;
    
        public Open(TV tv) {
            super();
            this.tv = tv;
        }
    
        /**
         * @see com.cathy.demo.pattern.command.Command#execute()
         */
        public void execute() {
            tv.open();
        }
    
    
    }
    

     
    /**
     *       -  
     * @author zhangwei_david
     * @version $Id: Close.java, v 0.1 2014 11 21    4:29:41 zhangwei_david Exp $
     */
    public class Close implements Command {
    
        private TV tv;
    
        /**
         * @see com.cathy.demo.pattern.command.Command#execute()
         */
        public void execute() {
            tv.close();
        }
    
        public Close(TV tv) {
            super();
            this.tv = tv;
        }
    
    }
    

     
    /**
     *
     * @author zhangwei_david
     * @version $Id: Change.java, v 0.1 2014 11 21    4:31:13 zhangwei_david Exp $
     */
    public class Change implements Command {
    
        /**  **/
        private int channel;
    
        /**
         * Getter method for property <tt>channel</tt>.
         *
         * @return property value of channel
         */
        public int getChannel() {
            return channel;
        }
    
        /**
         * Setter method for property <tt>channel</tt>.
         *
         * @param channel value to be assigned to property channel
         */
        public void setChannel(int channel) {
            this.channel = channel;
        }
    
        /**
         * @see com.cathy.demo.pattern.command.Command#execute()
         */
        public void execute() {
            System.out.println("    " + channel + "   ");
        }
    
        public Change(int channel, TV tv) {
            super();
            this.channel = channel;
        }
    
    }
    

     
    /**
     *    -   
     * @author zhangwei_david
     * @version $Id: Telecontroller.java, v 0.1 2014 11 21    4:39:00 zhangwei_david Exp $
     */
    public class Telecontroller {
    
        private Command open;
    
        private Command close;
    
        private Command one;
    
        private Command two;
    
        private Command three;
    
        public Telecontroller(TV tv) {
            open = new Open(tv);
            close = new Close(tv);
            one = new Change(1, tv);
            two = new Change(2, tv);
            three = new Change(3, tv);
        }
    
        /**
         *      
         */
        public void open() {
            open.execute();
        }
    
        /**
         *      
         */
        public void close() {
            close.execute();
        }
    
        /**
         *      1
         */
        public void one() {
            one.execute();
        }
    
        /**
         *      2
         */
        public void two() {
            two.execute();
        }
    
        /**
         *      3
         */
        public void three() {
            three.execute();
        }
    }
    

     
    /**
     *
     * @author zhangwei_david
     * @version $Id: Client.java, v 0.1 2014 11 21    4:40:19 zhangwei_david Exp $
     */
    public class Client {
        public static void main(String[] args) {
            //      
            SonyTV sonyTV = new SonyTV();
            //            
            Telecontroller telecontroller = new Telecontroller(sonyTV);
            //          
            telecontroller.open();
            //        1  
            telecontroller.one();
            //        3  
            telecontroller.three();
            //          
            telecontroller.close();
    
        }
    }
    

     
         
        1   
        3