Java design patterns [13] - Command Mode

Command Mode is a pattern of behavior, encapsulated into objects by the operation, the decoupling between the command and the command receiver requester. The caller simply need to give orders and then wait for the command to complete, on how to deal with actors command simply do not care.

Command Mode roles:

  • Command(Command interface): for all commands declare a public interface that defines a execute()method.
  • ConcreteCommand(Specific command): Command implemented on a specific interface, the command defines the operation and specific performer.
  • Invoker(Command requester): holds a specific command object, call the command object at a point in time execute()method.
  • Receiver(Command receivers): to receive and execute commands.
  • Client(Client):

UMLFIG class:
Here Insert Picture Description
In the remote control open / close television as an example.

ICommandinterface:

public interface ICommand {
    
    void execute();
}

ReceiverThat is the real executor command, here is the TV and lights.

Tv

public class Tv {

    public void on() {
        System.out.println("打开电视.");
    }

    public void off() {
        System.out.println("关闭电视.");
    }
}

Light

public class Light {

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

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

There are four specific commands: turn on the TV, the TV off, turn on the light, turn off the lamp.
TvOnCommand:

public class TvOnCommand implements ICommand {

    private Tv tv;

    public TvOnCommand(Tv tv) {
        this.tv = tv;
    }

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

TvOffCommand

public class TvOffCommand implements ICommand {

    private Tv tv;

    public TvOffCommand(Tv tv) {
        this.tv = tv;
    }

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

LightOnCommand

public class LightOnCommand implements ICommand {

    private Light light;

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

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

LightOffCommand

public class LightOffCommand implements ICommand {

    private Light light;

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

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

InvokerHere is the remote control, which holds a specific command object and invoke the command at some point in time.
RemoteControl:

public class RemoteControl {

    public static final int TV_ON = 0;
    public static final int TV_OFF = 1;
    public static final int LIGHT_ON = 2;
    public static final int LIGHT_OFF = 3;

    private List<ICommand> commands;

    public RemoteControl() {
        commands = new ArrayList<>(4);
        Tv tv = new Tv();
        commands.add(new TvOnCommand(tv));
        commands.add(new TvOffCommand(tv));
        Light light = new Light();
        commands.add(new LightOnCommand(light));
        commands.add(new LightOffCommand(light));
    }

    public void action(int i) {
        if (i < 0 || i > 3) {
            return;
        }
        commands.get(i).execute();
    }
}

The client by "pressing" the remote control to trigger a command:
Client:

public class Client {

    public static void main(String[] args) {
        RemoteControl remoteControl = new RemoteControl();
        remoteControl.action(RemoteControl.TV_ON);
        remoteControl.action(RemoteControl.TV_OFF);
        remoteControl.action(RemoteControl.LIGHT_ON);
        remoteControl.action(RemoteControl.LIGHT_OFF);
    }
}

Data results:
Here Insert Picture Description

Published 178 original articles · won praise 152 · views 610 000 +

Guess you like

Origin blog.csdn.net/hbtj_1216/article/details/104293032