【设计模式】行为型模式2——命令模式

命令模式类图
命令模式主要实现接受者(Receiver)与调用者(Invoker)之间的解耦,两者之间不是传统的调用者依赖接受者执行,而是通过具体的命令对象(ConcreteCommand)

1,优点:

  1. 降低系统的耦合度。命令模式能将调用操作的对象与实现该操作的对象解耦。
  2. 增加或删除命令非常方便。采用命令模式增加与删除命令不会影响其他类,它满足“开闭原则”,对扩展比较灵活。
  3. 可以实现宏命令。命令模式可以与组合模式结合,将多个命令装配成一个组合命令,即宏命令。 方便实现 Undo 和 Redo操作。
  4. 命令模式可以与后面介绍的备忘录模式结合,实现命令的撤销与恢复。

2,缺点:

  1. 使用命令模式可能会导致某些系统有过多的具体命令类。
  2. 系统结构更加复杂。

3, 使用场景

  1. 系统需要将请求调用者和请求接收者解耦,使得调用者和接收者不直接交互。
  2. 系统需要在不同的时间指定请求、将请求排队和执行请求。
  3. 系统需要支持命令的撤销(Undo)操作和恢复(Redo)操作。

原文链接https://blog.youkuaiyun.com/glass__sky/article/details/125224897

/**
 * 角色(Receiver)
 */
public class Light {

    public void on(){
        System.out.println("开灯......");
    }

    public void off(){
        System.out.println("关灯......");
    }
}

/**
 * 角色(Command)
 */
public abstract class Command {

    public abstract void execute();
}


/**
 * 角色(ConcreteCommand)
 */
public class LightOnCommand extends Command{

    private Light light;

    public LightOnCommand(Light light) {
        this.light = light;
    }

    @Override
    public void execute() {
        light.on();
    }
}


/**
 * 角色(ConcreteCommand)
 */
public class LightOffCommand extends Command{
    
    private Light light;

    public LightOffCommand(Light light) {
        this.light = light;
    }

    @Override
    public void execute() {
        light.off();
    }
}


/**
 * 空命令,方便封装
 */
public class EmptyCommand extends Command{
    @Override
    public void execute() {
        System.out.println("暂无操作");
    }
}

/**
 * 角色(Invoker)
 */
public class Controller {

    private Command[] onCommand;
    private Command[] offCommand;

    public Controller() {
        this.onCommand = new Command[5];
        this.offCommand = new Command[5];

        for (Command command : onCommand) {
            command = new EmptyCommand();
        }

        for (Command command : offCommand) {
            command = new EmptyCommand();
        }
    }

    public void setCommand(int index, Command onCommand, Command offCommand){
        this.onCommand[index] = onCommand;
        this.offCommand[index] = offCommand;
    }

    public void on(int index){
        this.onCommand[index].execute();
    }

    public void off(int index){
        this.offCommand[index].execute();
    }
}

测试类

public class Test {

    public static void main(String[] args) {
        Controller controller = new Controller();
        Light light = new Light();
        controller.setCommand(0, new LightOnCommand(light), new LightOffCommand(light));
        controller.on(0);
        controller.off(0);
    }
}

结果
命令模式结果

jdk源码应用——runnable

public class JdkInstance {

    public static void main(String[] args) {

        Thread thread = new Thread() {
            @Override
            public void run() {
                System.out.println("woca");
            }
        };
        // 显然此处调用命令
        thread.start();
    }
}

进入thread.start()

    public synchronized void start() {
        /**
         * This method is not invoked for the main method thread or "system"
         * group threads created/set up by the VM. Any new functionality added
         * to this method in the future may have to also be added to the VM.
         *
         * A zero status value corresponds to state "NEW".
         */
        if (threadStatus != 0)
            throw new IllegalThreadStateException();

        /* Notify the group that this thread is about to be started
         * so that it can be added to the group's list of threads
         * and the group's unstarted count can be decremented. */
        group.add(this);

        boolean started = false;
        try {
            start0();
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */
            }
        }
    }

    private native void start0();

通过本地方法start0()开启线程,等待系统调用
runnable相当于是抽象命令,Thread相当于具体调用者,通过匿名内部类创建的Thread相当于是组合了具体命令的调用者

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值