Command pattern definition: Encapsulates a request into an object so that different requests, queues, or logs are used to parameterize other objects.
- The command mode decouples the requesting object (Remotecontrol remote control Class) from the object executing the request (illumination Class);
- Communicate between the decoupled objects by command object (for example, the light class Lightcommand and the Turn off command);
- The caller makes a request by invoking the command execute (), and execute () invokes the actual performer;
- The caller can receive command arguments, even dynamically at run time (dynamically binds the command to the caller's remote, and the caller's remote is programmed for the interface);
- Command can support undo undo operation, on the caller (remote control) can record the last executed command, call undo undo the last command;
- Command mode enables logging and queuing systems:
Log system: Each command executed can be logged to the log system, when the system crashes or freezes, the system can be re-executed according to the command of the log system to restore; Queue system: Work queue, the actual work object and the caller decoupling, such as: thread pool, etc.; Defining a command interface: a way to standardize the implementation of each command
Public Interface Command { void execute (); void undo ();}
Define LAMP class: Under the light includes open and close two commands.
Public class Light { private String name; Public Light (String name) { this. Name = name; } Public void On () { System.out.println ("open Light"); } Public void off () { System.out.println ("Off Light");} }
Define the Light command class: The light-on and off-lights command for encapsulating lights.
Public class Implements Command { private light light ; Public Lightcommand (light) { this. Light = light ; } @Override publicvoid execute () { light.on (); } @Override publicvoid undo () { light.off (); }}
Defines the Nocommand command for numeric keys that do not have commands on the remote.
Public class Implements Command { public Nocommand () { } @Override publicvoid Execute () { System.out.println ("no command opened"); } @Override publicvoid undo () { System.out.println ("no Close Command") ; }}
Defines the remote control class: Defines an array of commands to be stored, and two commands for opening and closing a user;
Public classRemotecontrol {command[] commands; PublicRemotecontrol () {commands=NewCommand[10]; for(inti=0;i<10;i++) {Commands[i]=NewNocommand (); } } Public voidSetcommands (intSlot,command OnCommand) {Commands[slot]=OnCommand; } Public voidOnbutton (intslot) {Commands[slot].execute (); } Public voidOffbutton (intslot) {Commands[slot].undo (); }}
Test class:
Public classTest { Public Static voidMain (string[] args) { light light=NewLight ("Electric bulb"); Lightcommand Lightcommand=NewLightcommand (light); Remotecontrol Control=NewRemotecontrol (); Control.setcommands (0, Lightcommand); Control.onbutton (0); Control.offbutton (0); Control.onbutton (1); Control.offbutton (1); }} results: Turn off the lights without opening the command without closing the command
6. Command mode