java 命令模式

来源:互联网 发布:区域分割算法优缺点 编辑:程序博客网 时间:2024/06/17 17:59

别名:
Action,Transaction
参与者:

Command

——声明执行操作的接口

ConcreteCommand

——将一个接收者对象绑定于一个动作。
——调用接收者相应的操作,以实现execute

Client

——创建一个具体命令对象并设定它的接收者

Invoker

——要求该命令执行这个请求

Receiver

——知道如何实施与一个请求相关的操作。任何类都可能作为一个接收者

协作

——Client创建一个ConcreteCommand对象并指定它的Receiver对象
——某Invoker对象存储该ConcreteCommand对象
——该Invoker通过调用Command对象的Execute操作来提交一个请求,如果该命令是可以撤销的,ConcreteCommand就在执行Excute操作之前存储当前状态以用于取消该命令。
——ConcreteCommand对象调用它的Receiver的一些操作以执行该请求。

结构图:

这里写图片描述
效果:
1)Command模式支持将调用操作对象与知道如何实现该操作的对象解耦。
2)Command是头等对象。它们可以像其他对象一样被操纵和拓展。
3)你可以将多个命令装配为一个复合命令。
4)增加新的Command很容易,因为这不需要改变已有的类。

package hello;interface Command{    public void Execute();    public void Undo();}class ConcreteCommand implements Command{    Receiver receiver=new Receiver();    private String prestate;    public ConcreteCommand(Receiver receiver) {        super();        this.receiver = receiver;    }    @Override    public void Execute() {        System.out.println("执行操作开始");        System.out.println("现在的Receiver的State为"+receiver.getState());        this.prestate=this.receiver.getPreState();        System.out.println("执行完成");        this.receiver.action();        System.out.println("现在的Receiver的State为"+receiver.getState());        System.out.println("执行操作完成");    }    @Override    public void Undo() {        System.out.println("撤销操作开始");        System.out.println("现在的Receiver的State为"+receiver.getState());        this.receiver.unAction();        System.out.println("撤销完成");        this.receiver.setState(this.prestate);        System.out.println("现在的Receiver的State为"+receiver.getState());        System.out.println("撤销操作完成");    }}class Receiver {    public static final String state1="state1";    public static final String state2="state2";    public static final String state3="state3";    private String state;    public Receiver() {        super();        this.state = state1;    }    public String getPreState() {        return this.state;    }    public String getState() {        return this.state;    }    public void setState(String state) {        this.state=state;    }    public void action(){       // System.out.println("执行");        this.state=state2;        //System.out.print("the state now is "+this.state);    }    public void unAction(){        //System.out.println("撤销");       // System.out.print("the state now is "+this.state);    }}class Invoker {    private Command command;    public Invoker(Command command) {        super();        this.command = command;    }    public void ExecuteCommand(){        command.Execute();    }    public void UndoCommand(){        command.Undo();    }}public class CommandMethod {    public static void main(String[] args) {         Receiver receiver = new Receiver();         Command command = new ConcreteCommand(receiver);         Invoker invoker = new Invoker(command);         invoker.ExecuteCommand();         invoker.UndoCommand();    }}

执行结果:

执行操作开始现在的Receiver的State为state1执行完成现在的Receiver的State为state2执行操作完成撤销操作开始现在的Receiver的State为state2撤销完成现在的Receiver的State为state1撤销操作完成