程序员

设计模式——命令模式

2018-01-10  本文已影响0人  BrightLoong
command
阅读原文请访问我的博客BrightLoong's Blog

一. 简介

命令模式 ,将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队记录请求日志,以及支持撤销的操作。

命令模式的特点是对命令进行了封装,将请求的具体操作封装成命令对象,用户无需知道具体需要执行什么样的操作逻辑,只用调用对应的命令即可,实现了用户请求和请求实现的解耦,方便扩展。

二. 模式结构解析

UML类图

UML图

三. 简单命令模式代码实现

1. ICommand命令接口定义

package brightloong.github.io.command.core.simple;
public interface ICommand {
    public void execute();
}

2. ConcreteCommand具体命令实现

package brightloong.github.io.command.core.simple.impl;

import brightloong.github.io.command.core.simple.ICommand;
import brightloong.github.io.command.core.simple.Receiver;

public class ConcreteCommand implements ICommand {
    
    private Receiver receiver;
    
    public ConcreteCommand(Receiver receiver) {
        this.receiver = receiver;
    }

    /** (non-Javadoc)
     * @see brightloong.github.io.command.core.simple.ICommand#execute()
     */
    public void execute() {
        System.out.println("ConcreteCommand发送命令给接给命令接收者!");
        receiver.action();
    }

}

3. Receiver具体的命令接收者实现

package brightloong.github.io.command.core.simple;

public class Receiver {
    
    public void action() {
        System.out.println("Receiver接收到命令并执行!");
    }

}

4. Invoker命令调用者

package brightloong.github.io.command.core.simple;

public class Invoker {
    
    private ICommand command;
    
    /**
     * @return the command
     */
    public ICommand getCommand() {
        return command;
    }

    public void setCommand(ICommand command) {
        this.command = command;
    }

    public void action() {
        System.out.println("命令请求者Invoker发起命令!");
        this.command.execute();
    }
}

5. Client客户端测试代码和结果

package brightloong.github.io.command.core.simple;

import brightloong.github.io.command.core.simple.impl.ConcreteCommand;


public class Client {
    public static void main(String[] args) {
        Receiver receiver = new Receiver();
        ICommand command = new ConcreteCommand(receiver);
        Invoker invoker = new Invoker();
        invoker.setCommand(command);
        invoker.action();
    }
}

输出结果如下:

命令请求者Invoker发起命令!
ConcreteCommand发送命令给接给命令接收者!
Receiver接收到命令并执行!

四. 宏命令的代码实现

宏命令,就是又多条命令组成一个命令,是一个命令的组合。实现如下

1. 新增宏命令抽象定义IMacroCommand

package brightloong.github.io.command.core.macro;

import brightloong.github.io.command.core.simple.ICommand;

public interface IMacroCommand extends ICommand{
    
    public void add(ICommand command);
    
    public void remove(ICommand command);
}

2. 宏命令具体实现MacroCommandImpl

package brightloong.github.io.command.core.macro.impl;

import java.util.ArrayList;
import java.util.List;

import brightloong.github.io.command.core.macro.IMacroCommand;
import brightloong.github.io.command.core.simple.ICommand;

public class MacroCommandImpl implements IMacroCommand{
    List<ICommand> commands = new ArrayList<ICommand>();

    /** (non-Javadoc)
     * @see brightloong.github.io.command.core.simple.ICommand#execute()
     */
    public void execute() {
        for (ICommand command : commands) {
            command.execute();
        }
    }

    /** (non-Javadoc)
     * @see brightloong.github.io.command.core.macro.IMacroCommand#add(brightloong.github.io.command.core.simple.ICommand)
     */
    public void add(ICommand command) {
        commands.add(command);
    }

    /** (non-Javadoc)
     * @see brightloong.github.io.command.core.macro.IMacroCommand#remove(brightloong.github.io.command.core.simple.ICommand)
     */
    public void remove(ICommand command) {
        commands.remove(command);
    }

}

3. Receiver新增方法

package brightloong.github.io.command.core.simple;

public class Receiver {
    
    public void action() {
        System.out.println("Receiver接收到命令并执行!");
    }
    
    public void sing() {
        System.out.println("大河向东流,天上的形象参北斗...");
    }

    public void playGame() {
        System.out.println("大吉大利,今晚吃鸡。");
    }
}

4. 新增命令PlayGameCommand和SingCommand


package brightloong.github.io.command.core.simple.impl;

import brightloong.github.io.command.core.simple.ICommand;
import brightloong.github.io.command.core.simple.Receiver;

public class PlayGameCommand implements ICommand {
    
    private Receiver receiver;
    
    public PlayGameCommand(Receiver receiver) {
        this.receiver = receiver;
    }
    
    /** (non-Javadoc)
     * @see brightloong.github.io.command.core.simple.ICommand#execute()
     */
    public void execute() {
        receiver.playGame();
    }

}


package brightloong.github.io.command.core.simple.impl;

import brightloong.github.io.command.core.simple.ICommand;
import brightloong.github.io.command.core.simple.Receiver;

public class SingCommand implements ICommand {
    
    private Receiver receiver;
    
    public SingCommand(Receiver receiver) {
        this.receiver = receiver;
    }
    
    /** (non-Javadoc)
     * @see brightloong.github.io.command.core.simple.ICommand#execute()
     */
    public void execute() {
        receiver.sing();
    }

}

5. 修改客户端Client以及输出结果展示

package brightloong.github.io.command.core.simple;

import brightloong.github.io.command.core.macro.IMacroCommand;
import brightloong.github.io.command.core.macro.impl.MacroCommandImpl;
import brightloong.github.io.command.core.simple.impl.PlayGameCommand;
import brightloong.github.io.command.core.simple.impl.SingCommand;

public class Client {
    public static void main(String[] args) {
        Receiver receiver = new Receiver();
        ICommand singCommand = new SingCommand(receiver);
        ICommand playGameCommand = new PlayGameCommand(receiver);
        
        IMacroCommand macroCommand = new MacroCommandImpl();
        macroCommand.add(singCommand);
        macroCommand.add(playGameCommand);
        
        Invoker invoker = new Invoker();
        invoker.setCommand(macroCommand);
        
        invoker.action();
    }
}

输出结果如下:

命令请求者Invoker发起命令!
大河向东流,天上的形象参北斗...
大吉大利,今晚吃鸡。

五. 使用场景

1. 应用场景

2. 具体场景

如果系统需要实现多级回退操作,这时如果所有用户的操作都以command对象的形式实现,系统可以简单地用stack来保存最近执行的命令,如果用户需要执行undo操作,系统只需简单地popup一个最近的 command对象然后执行它的undo()方法既可。

借助command模式,可以简单地实现一个具有原子事务的行为。当一个事务失败时,往往需要回退到执行前的状态,可以借助command对象保存这种状态,简单地处理回退操作。

假如系统需要按顺序执行一系列的命令操作,如果每个command对象都提供一个 getEstimatedDuration()方法,那么系统可以简单地评估执行状态并显示出合适的状态条。

通常一个使用多个wizard页面来共同完成一个简单动作。一个自然的方法是使用一个command对象来封装wizard过程,该command对象在第一个wizard页面显示时被创建,每个wizard页面接收用户输入并设置到该command对象中,当最后一个wizard页面用户按下“Finish”按钮时,可以简单地触发一个事件调用execute()方法执行整个动作。通过这种方法,command类不包含任何跟用户界面有关的代码,可以分离用户界面与具体的处理逻辑。

Swing系统里,用户可以通过工具条按钮,菜单按钮执行命令,可以用command对象来封装命令的执行。

通常一个典型的线程池实现类可能有一个名为addTask()的public方法,用来添加一项工作任务到任务队列中。该任务队列中的所有任务可以用command对象来封装,通常这些command对象会实现一个通用的接口比如java.lang.Runnable。

可以用command对象来封装用户的一个操作,这样系统可以简单通过队列保存一系列的command对象的状态就可以记录用户的连续操作。这样通过执行队列中的command对象,就可以完成"Play back"操作了。

通过网络发送command命令到其他机器上运行。

当一个调用共享某个资源并被多个线程并发处理时。

六. 优缺点

1. 优点

命令模式使得发起命令的对象——客户端,和具体实现命令的对象——接收者对象完全解耦,也就是说发起命令的对象完全不知道具体实现对象是谁,也不知道如何实现。

命令模式把请求封装起来,可以动态地对它进行参数化、队列化和日志化等操作,从而使得系统更灵活。

命令模式中的命令对象能够很容易地组合成复合命令,也就是宏命令,从而使系统操作更简单,功能更强大。

由于发起命令的对象和具体的实现完全解耦,因此扩展新的命令就很容易,只需要实现新的命令对象,然后在装配的时候,把具体的实现对象设置到命令对象中,然后就可以使用这个命令对象,已有的实现完全不用变化。

2. 缺点

参考

上一篇 下一篇

猜你喜欢

热点阅读