Command mode for reading HeadFirst Design Mode notes

Command mode: Encapsulate Requests as objects to parameterize other objects using different requests, queues, or logs.Command mode also supports undoable operations.

Four roles:

Command: Defines a uniform interface for commands

The implementer of the ConcreteCommand:Command interface, which is used to execute specific commands and, in some cases, to act directly as a Receiver.

Receiver: The actual executor of the command

Invoker: Requestor of a command, the most important role in command mode.This role is used to control individual commands.

Lift a chestnut:

To implement a remote control with smart switch, you can control the lights in the living room and bedroom, switch on the TV, and so on.

Define the command interface:

public interface Command {
    public void execute();
}

Implement the light switch command:

// Light on command
public class LightOnCommand implements Command {
    Light light;

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

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

// Light off command
public class LightOffCommand implements Command {
    Light light;

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

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

Remote control:

// The remote control has seven switches and the initialization is commandless
public class RemoteControl {
    Command[] onCommands;
    Command[] offCommands;

    // Initialize remote control
    public RemoteControl() {
        onCommands = new Command[7];
        offCommands = new Command[7];

        Command noCommand = new NoCommand();
        for (int i = 0; i < 7; i++) {
            onCommands[i] = noCommand;
            offCommands[i] = noCommand;
        }
    }

    // Configuration button corresponding command
    public void setCommand(int index, Command onCommand, Command offCommand) {
        onCommands[index] = onCommand;
        offCommands[index] = offCommand;
    }

    // Press the open button
    public void onButtonWasPushed(int index) {
        onCommands[index].execute();
    }

    // Press the off button
    public void offButtonWasPushed(int index) {
        onCommands[index].execute();
    }
}

Test the remote control:

public class RemoteTest {
    RemoteControl remoteControl = new RemoteControl();

    Light livingRoomLight = new Light("Living Room");
    Light kitchenLight = new Light("kitchen");

    // Set remote control button command
    LightOnCommand livingRoomLightOnCommand = new LightOnCommand(livingRoomLight);
    LightOffCommand livingRoomLightOffCommand = new LightOffCommand(livingRoomLight);

    LightOnCommand kitchenLightOnCommand = new LightOnCommand(kitchenLight);
    LightOffCommand kitchenLightOffCommand = new LightOffCommand(kitchenLight);

    remoteControl.setCommand(0, livingRoomLightOnCommand, livingRoomLightOffCommand);
    remoteControl.setCommand(1, kitchenLightOnCommand, kitchenLightOffCommand);

    // Bedroom light switch operation
    remoteControl.onButtonWasPushed(0);
    remoteControl.offButtonWasPushed(0);
    // Kitchen light switch operation
    remoteControl.onButtonWasPushed(1);
    remoteControl.offButtonWasPushed(1);
}

Posted by dotnotwhat on Sun, 05 Jan 2020 05:15:03 -0800