您的位置:首页 > 其它

命令模式

2008-03-05 00:27 225 查看
 

1 命令的接受者和其命令动作:


public class Receiver




...{


    public Receiver()




    ...{


        //write code here


    }




    public void action()




    ...{


        System.out.println("Action has been taken.");


    }


}

 2 抽象命令




public interface Command ...{


    void execute();


}

 

3 具体命令的对象绑定和执行


public class ConcreteCommand implements Command




...{


    public ConcreteCommand(Receiver receiver)




    ...{


        this.receiver = receiver;


    }




    public void execute()




    ...{


        receiver.action();


    }






    /** *//**


     * @directed


     * @clientRole receiver


     */


    private Receiver receiver;


}

4 请求者的命令调用


public class Invoker




...{


    public Invoker(Command command)




    ...{


        this.command = command;


    }




    public void action()




    ...{


        command.execute();


    }






    /** *//**


     * @link aggregation


     * @directed 


     */


    private Command command;


}

5 客户端调用


public class Client




...{


    public static void main(String[] args)




    ...{
// 创建命令的接受者


        Receiver receiver = new Receiver();
// 命令对象 指定接受者


        Command command = new ConcreteCommand(receiver);
//命令执行对象 设定命令


        Invoker invoker = new Invoker( command );


//执行命令


        invoker.action();


    }






    /** *//** @link dependency */




    /**//*#Receiver lnkReceiver;*/






    /** *//** @link dependency */




    /**//*#Invoker lnkInvoker;*/


}



 

init:
deps-jar:
compile-single:
run-single:
Action has been taken.
生成成功(总时间:1 秒)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息