Design Mode-command mode, design mode command

Source: Internet
Author: User

Design Mode-command mode, design mode command

Definition of command mode: encapsulate a "request" into an object to parameterize other objects using different requests, queues, or logs. The command mode also supports undo operations. Note that the command mode encapsulates requests into objects!

In fact, simply put, the command mode encapsulates method calls. By encapsulating method calls, you can package the computing block, therefore, the object that calls this operation does not need to care about how things are done, as long as you know how to use the packaging method to complete it.

Command mode: When you need to decouple the requested object from the object that executes the request, consider using the command mode.

Scenario example: design a multi-function remote control that can control the status of each electrical appliance in the home, such as the switch of the electric lamp, switch of the TV, speed of the fan, switch of the garage door, etc, the following uses the command mode to implement the remote control. Here, the request object is the remote control, and the request object is various electrical appliances.

First, let all the command objects implement the same command interface.

public interface Command {    public void execute();    public void undo();}

There are two methods in the interface, one is the execute Method for executing the action, and the other is the undo Method for revoking the action;

The following command first declares an electric lamp before enabling or disabling the lamp.

public class Light {        protected void on() {        System.out.println("light is on");    }    protected void off() {        System.out.println("light is off");    }}

There are only two methods for electric lights: on, off, and off.

public class LightOnCommand implements Command {    Light light;    public LightOnCommand(Light light) {        // TODO Auto-generated constructor stub        this.light = light;    }    @Override    public void execute() {        // TODO Auto-generated method stub        light.on();    }    @Override    public void undo() {        // TODO Auto-generated method stub        light.off();    }}

Note that the undo operation is the opposite of the execute operation; write a command to turn off the light

public class LightOffCommand implements Command {    Light light;    public LightOffCommand(Light light) {        // TODO Auto-generated constructor stub        this.light = light;    }    @Override    public void execute() {        // TODO Auto-generated method stub        light.off();    }    @Override    public void undo() {        // TODO Auto-generated method stub        light.on();    }}

Add another garage door opening and closing

public class GarageDoor {    public void up() {        System.out.println("the garagedoor is up!");    }    public void down() {        System.out.println("the garagedoor is down!");    }    public void lightOn() {        System.out.println("the garagedoor's light is on!");    }    public void lightOff() {        System.out.println("the garagedoor's light is off!");    }}
public class GarageDoorOpenCommand implements Command {    GarageDoor garageDoor;    public GarageDoorOpenCommand(GarageDoor garageDoor) {        // TODO Auto-generated constructor stub        this.garageDoor = garageDoor;    }    @Override    public void execute() {        // TODO Auto-generated method stub        garageDoor.up();        garageDoor.lightOn();    }    @Override    public void undo() {        // TODO Auto-generated method stub        garageDoor.down();        garageDoor.lightOff();    }}
public class GarageDoorCloseCommand implements Command {    GarageDoor garageDoor;    public GarageDoorCloseCommand(GarageDoor garageDoor) {        // TODO Auto-generated constructor stub        this.garageDoor = garageDoor;    }    @Override    public void execute() {        // TODO Auto-generated method stub        garageDoor.down();        garageDoor.lightOff();    }    @Override    public void undo() {        // TODO Auto-generated method stub        garageDoor.up();        garageDoor.lightOn();    }}

Then the remote control is implemented.

Public class RemoteControl {/** because multiple appliances use arrays to record these commands */Command [] onCommands; Command [] offCommands; Command undoCommand; // used to record the revocation Command public RemoteControl () {// TODO Auto-generated constructor stub onCommands = new Command [7]; offCommands = new Command [7]; command noCommand = new NoCommand (); // The Initialization is empty. for (int I = 0; I <7; I ++) {onCommands [I] = noCommand; offCommands [I] = noCommand;} undoCommand = noCommand;} public void setCommand (int slot, Command onCommand, Command offCommand) {onCommands [slot] = onCommand; offCommands [slot] = offCommand;} public void onButtonWasPushed (int slot) {oncommands[slot0000.exe cute (); undoCommand = onCommands [slot];} public void offButtonWasPushed (int slot) {offCommands[slot].exe cute (); undoCommand = offCommands [slot];} public void undoButtonWasPushed () {undoCommand. undo ();}}

The empty command does not do anything here, but ensures that all the buttons on the command object exist. The implementation of the empty command

public class NoCommand implements Command {    @Override    public void execute() {        // TODO Auto-generated method stub        System.out.println("no command!");    }    @Override    public void undo() {        // TODO Auto-generated method stub            }}

Test the remote control.

public class RemoteLoader {    public static void main(String[] args) {        // TODO Auto-generated method stub        RemoteControl remoteControl=new RemoteControl();        Light light=new Light();        LightOnCommand lightOn=new LightOnCommand(light);        LightOffCommand lightOff =new LightOffCommand(light);                GarageDoor garageDoor=new GarageDoor();        GarageDoorOpenCommand garageDoorOpen=new GarageDoorOpenCommand(garageDoor);        GarageDoorCloseCommand garageDoorClose=new GarageDoorCloseCommand(garageDoor);                remoteControl.setCommand(0, lightOn, lightOff);        remoteControl.setCommand(1, garageDoorOpen, garageDoorClose);                remoteControl.onButtonWasPushed(0);        remoteControl.onButtonWasPushed(1);        remoteControl.offButtonWasPushed(0);        remoteControl.offButtonWasPushed(1);    }}

Print result:

light is onthe garagedoor is up!the garagedoor's light is on!light is offthe garagedoor is down!the garagedoor's light is off!

After the remote control is implemented, test the undo command.

public class RemoteLoader {    public static void main(String[] args) {        // TODO Auto-generated method stub        RemoteControl remoteControl=new RemoteControl();        Light light=new Light();        LightOnCommand lightOn=new LightOnCommand(light);        LightOffCommand lightOff =new LightOffCommand(light);                remoteControl.setCommand(0, lightOn, lightOff);                remoteControl.onButtonWasPushed(0);        System.out.println("****************");        remoteControl.undoButtonWasPushed();    }}

Print results

light is on****************light is off

Summary:

(1) In command mode, the requested object and the requested object are decoupled;

(2) the decoupled two communicate through command objects. The command object encapsulates the receiver and one or more actions.

(3) The caller sends a request by calling execute () of the command object, which will call the recipient's action.

(4) The caller can accept the command as a parameter, or even dynamically perform it at runtime.

(5) The command can be undone by implementing an undo () method to return to the status before the last execute () is executed.

   

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.