设计模式系列:命令模式

概念

命令模式将命令的执行和发送命令的责任分隔开,委派给不同的对象执行。

  • ###涉及角色
  1. 客户端(Client)角色:创建一个具体命令(ConcreteCommand)对象并确定其接收者。
  2. 命令(Command)角色:声明了一个给所有具体命令类的抽象接口。
  3. 具体命令(ConcreteCommand)角色:定义一个接收者和行为之间的弱耦合;实现execute()方法,负责调用接收者的相应操作。execute()方法通常叫做执行方法。
  4. 请求者(Invoker)角色:负责调用命令对象执行请求,相关的方法叫做行动方法。
  5. 接收者(Receiver)角色:负责具体实施和执行一个请求。任何一个类都可以成为接收者,实施和执行请求的方法叫做行动方法。

实现

类图

图片

图片

代码

  • 客户端
    FishCommander

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    package com.littlehui.design.command;

    /**
    * @Description 鱼长官
    * @ClassName Client
    * @Author littlehui
    * @Date 2020/1/8 16:29
    * @Version 1.0
    **/
    public class FishCommander {

    public static void main(String[] args) {
    FishSoldier fishSoldier = new FishSoldier();
    Command huntingFishCommand = new HuntingFishCommand(fishSoldier);
    Command cookingFishCommand = new CookingFishCommand(fishSoldier);
    CommandBrodCast commandBrodCast = new CommandBrodCast();
    commandBrodCast.setCookingFishCommand(cookingFishCommand);
    commandBrodCast.setHuntingFishCommand(huntingFishCommand);
    commandBrodCast.huntingFish();
    commandBrodCast.cookingFish();
    }
    }
  • 命令
    Command接口

    1
    2
    3
    4
    5
    6

    package com.littlehui.design.command;

    public interface Command {
    public void execute();
    }

HuntingFishCommand

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.littlehui.design.command;

/**
* @Description TODO
* @ClassName HuntingFish
* @Author littlehui
* @Date 2020/1/8 16:31
* @Version 1.0
**/
public class HuntingFishCommand implements Command {

FishSoldier fishSoldier;

public HuntingFishCommand(FishSoldier fishSoldier) {
this.fishSoldier = fishSoldier;
}

public void execute() {
fishSoldier.doHuntingFish();
}
}

CookingFishCommand

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.littlehui.design.command;

/**
* @Description TODO
* @ClassName CookingFishCommand
* @Author littlehui
* @Date 2020/1/8 16:32
* @Version 1.0
**/
public class CookingFishCommand implements Command {

FishSoldier fishSoldier;

public CookingFishCommand(FishSoldier fishSoldier) {
this.fishSoldier = fishSoldier;
}

public void execute() {
fishSoldier.doCookingFish();
}
}
  • 命令发送者请求者

CommandBrodCast

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.littlehui.design.command;

/**
* @Description TODO
* @ClassName CommandInvoder
* @Author littlehui
* @Date 2020/1/8 16:34
* @Version 1.0
**/
public class CommandBrodCast {

private Command huntingFishCommand;

private Command cookingFishCommand;

public void setCookingFishCommand(Command cookingFishCommand) {
this.cookingFishCommand = cookingFishCommand;
}

public void setHuntingFishCommand(Command huntingFishCommand) {
this.huntingFishCommand = huntingFishCommand;
}


public void cookingFish() {
cookingFishCommand.execute();
}

public void huntingFish() {
huntingFishCommand.execute();
}
}
  • 命令接收者

FishSoldier

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.littlehui.design.command;

/**
* @Description TODO
* @ClassName Soldier
* @Author littlehui
* @Date 2020/1/8 16:38
* @Version 1.0
**/
public class FishSoldier {

public void doHuntingFish() {
System.out.println("hunting fish");
}

public void doCookingFish() {
System.out.println("cooking fish");
}
}

场景

命令模式的场景经常用在对外系统调用的时候使用。比如一个http请求封装成一个命模式。
不同的参数也可以封装成不同的命令。命令执行端,服务端的业务发生改变时或者url调用
方式发生改变时,客户端不需要调整。只要修改相应的命令封装器就好了。有效地做到
了修改的隔离。

总结

命令模式的最大特点就是隔离了命令的发送端和执行端。命令的发送端无需知道命令
执行端具体怎么执行。这种隔离方式的好处是调用的透明化。具有更好的扩展性。
由于命令是一个个相互独立的,所以可以很自然的做到复合命令。命令已经是封装
过的,所以可以对他进行有效的参数化。综合以上他有几种特点:

  1. 更松散的耦合
  2. 更动态的控制
  3. 支持复合命令
  4. 更好的扩展性