10.行为型 - 命令模式 (Command)

1.定义

  • 将请求封装为一个对象(Invoker),命令(Command)作为请求对象的参数
  • 增加类的复杂度来达到解耦调用关系的目的

2.UML类图

在这里插入图片描述

  • 角色介绍
    Receiver : 接收者角色,执行请求的具体逻辑都在此类中
    ICommand : 抽象命令接口
    ConcreteCommand : 具体命令角色,内部持有Receiver引用,调用Receiver的方法实现功能
    Invoker : 请求者角色,持有对应命令引用,调用命令对象方法完成请求
  • 要点
    Receiver是具体命令请求的实现类;
    请求者调用 体命令的实现,命令调用Receiver的实现。

3.UML示例代码

/**
 * Copyright (C), 2016-2020
 * FileName: MachineReceiver
 * Author: wei.zheng
 * Date: 2019/12/13 21:16
 * Description: MachineReceiver, 接收者角色,实现具体业务逻辑
 */
public class MachineReceiver {
    public void toUp() {
        System.out.println("UP");
    }

    public void toDown() {
        System.out.println("DOWN");
    }

    public void toLeft() {
        System.out.println("LEFT");
    }

    public void toRight() {
        System.out.println("RIGHT");
    }
}
/**
 * Copyright (C), 2016-2020
 * FileName: ICommand
 * Author: wei.zheng
 * Date: 2019/12/13 21:14
 * Description: 命令接口
 */
public interface ICommand {
    void execute();
}
/**
 * Copyright (C), 2016-2020
 * FileName: UpCommand
 * Author: wei.zheng
 * Date: 2019/12/13 21:18
 * Description: UpCommand,持有Receiver引用,调用Receiver的方法实现请求
 */
public class UpCommand implements ICommand {
    private MachineReceiver machineReceiver;

    public UpCommand(MachineReceiver machineReceiver) {
        this.machineReceiver = machineReceiver;
    }

    @Override
    public void execute() {
        machineReceiver.toUp();
    }
}
/**
 * Copyright (C), 2016-2020
 * FileName: ControllerInvoker
 * Author: wei.zheng
 * Date: 2019/12/15 9:22
 * Description: 控制器Invoker,求请者角色,持有对应具体命令引用,调用命令方法完成请求
 */
public class ControllerInvoker {
    private ICommand upCommand;
    private ICommand downCommand;
    private ICommand leftCommand;
    private ICommand rightCommand;

    public void setUpCommand(ICommand upCommand) {
        this.upCommand = upCommand;
    }

    public void setDownCommand(ICommand downCommand) {
        this.downCommand = downCommand;
    }

    public void setLeftCommand(ICommand leftCommand) {
        this.leftCommand = leftCommand;
    }

    public void setRightCommand(ICommand rightCommand) {
        this.rightCommand = rightCommand;
    }

    public void toUp() {
        upCommand.execute();
    }

    public void toDown() {
        downCommand.execute();
    }

    public void toLeft() {
        leftCommand.execute();
    }

    public void toRight() {
        rightCommand.execute();
    }
}
/**
 * Copyright (C), 2016-2020
 * FileName: Client2
 * Author: wei.zheng
 * Date: 2019/12/15 9:25
 * Description: 命令模式用户使用类
 */
public class Client2 {

    public static void main(String[] args) {
        MachineReceiver machineReceiver = new MachineReceiver();

        UpCommand upCommand = new UpCommand(machineReceiver);
        DownCommand downCommand = new DownCommand(machineReceiver);
        LeftCommand leftCommand = new LeftCommand(machineReceiver);
        RightCommand rightCommand = new RightCommand(machineReceiver);

        ControllerInvoker invoker = new ControllerInvoker();
        invoker.setUpCommand(upCommand);
        invoker.setDownCommand(downCommand);
        invoker.setLeftCommand(leftCommand);
        invoker.setRightCommand(rightCommand);

        invoker.toUp();
        invoker.toDown();
        invoker.toLeft();
        invoker.toRight();
    }
}
// 运行结果
2019-12-15 20:17:46.671 14552-14552/com.example.command I/System.out: UP
2019-12-15 20:17:46.671 14552-14552/com.example.command I/System.out: DOWN
2019-12-15 20:17:46.672 14552-14552/com.example.command I/System.out: LEFT
2019-12-15 20:17:46.672 14552-14552/com.example.command I/System.out: RIGHT

4.对策略模式相比有无异同

宏观的理解:
策略模式,对同一个动作或者算法有不同的实现;
命令模式,针对不同命令,由不同的接收者方法来实现。

5.总结

  • 优缺点
    优点:扩展更灵活
    缺点:类的膨胀非常大
发布了37 篇原创文章 · 获赞 0 · 访问量 573

猜你喜欢

转载自blog.csdn.net/qq_37514242/article/details/103552741
今日推荐