java23种设计模式—命令模式

在软件系统中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”。但在某些场合,比如要对行为进行“记录、撤销/重做、事务”等处理,这种无法抵御变化的紧耦合是不合适的。在这种情况下,如何将“行为请求者”与“行为实现者”解耦?将一组行为抽象为对象,实现二者之间的松耦合。这就是命令模式(Command Pattern)。

定义

将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。

角色

  • 抽象命令 定义了一系列命令的操作
  • 命令接受者(真正执行命令所要求的对象)
  • 具体命令 抽象命令的实现,拥有一个(has a)真正的命令接受者
  • 命令请求者 发出命令的对象,拥有一个(has a)具体命令对象

UML

实现

/**
 * desc : 命令接口
 * Created by tiantian on 2018/8/11
 */
public interface Command {
    void excute(); 
}

/**
 * desc : 命令接收者
 * Created by tiantian on 2018/8/11
 */
public class Receiver {
    public void action() {
        System.out.println(this.getClass().getCanonicalName() + " do action");
    }
}

/**
 * desc : 具体命令
 * Created by tiantian on 2018/8/11
 */
public class ConcreteCommand implements Command {
    private Receiver receiver;

    public ConcreteCommand(Receiver receiver) {
        this.receiver = receiver;
    }

    public void excute() {
        receiver.action();
    }
}
/**
 * desc : 命令请求者
 * Created by tiantian on 2018/8/11
 */
public class Invoker {

    private Command command;

    public Invoker(Command command) {
        this.command = command;
    }
    
    public void action() {
        command.excute();
    }
}
/**
 * desc : 测试客户端
 * Created by tiantian on 2018/8/11
 */
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();
    }
    
    // out put : design.patten.command.Receiverdo action
}

示例

以平时常操作的垃圾文件回收和撤销为例。在抽象命令接口增加withdraw()命令撤回方法。

/**
 * desc : 抽象命令接口
 * Created by tiantian on 2018/8/11
 */
public interface Command {
    void excute(); 
    
    void withdraw();
}

真正处理垃圾回收撤销等命令的对象

/**
 * desc : 垃圾回收命令处理器
 * Created by tiantian on 2018/8/11
 */
public class GarbageHandler {
    public void handleDiscard() {
        System.out.println("handleDiscard() method will recycle garbage.");
    }
    
    public void getBack() {
        System.out.println("getBack() method will get back the discarded. ");
    }
}
/**
 * desc : 垃圾回收命令
 * Created by tiantian on 2018/8/11
 */
public class GarbageRecycleCommand implements Command {
    private final GarbageHandler receiver;

    public GarbageRecycleCommand(GarbageHandler receiver) {
        this.receiver = receiver;
    }

    @Override
    public void excute() {
        receiver.handleDiscard();
    }

    @Override
    public void withdraw() {
        receiver.getBack();
    }
}

垃圾回收命令请求者

/**
 * desc : 命令请求者
 * Created by tiantian on 2018/8/11
 */
public class Invoker {

    private Command command;

    public Invoker(Command command) {
        this.command = command;
    }
    
    public void action() {
        command.excute();
    }
    
    public void withdraw() {
        command.withdraw();
    }
}
/**
 * desc : 垃圾回收测试客户端
 * Created by tiantian on 2018/8/11
 */
public class Client {
    public static void main(String[] args) {
        GarbageHandler handler = new GarbageHandler();
        Command command = new GarbageRecycleCommand(handler);
        Invoker invoker = new Invoker(command);
        
        invoker.action();
        invoker.withdraw();
    }
    
    // handleDiscard() method will recycle garbage.
    // getBack() method will get back the discarded. 
}

总结

命令模式隔离了命令请求者和命令执行者,这中间的枢纽就是命令角色,命令角色连接了命令请求者和执行者。这样的好处显而易见,极大的降低了命令请求者和执行者的耦合程度。

猜你喜欢

转载自blog.csdn.net/m0_37577221/article/details/82779842