[Design mode] Command mode)

Source: Internet
Author: User

The command mode is defined as: encapsulate a request into an object, so you can use different requests to parameterize other objects, add requests to the queue or record request logs, and support revocation.
Official definition: The Command Pattern encapsulates a request as an object, thereby letting you parameterize other objects with different request, queue or log requests, and support undoable operations.


Important participants in Command:
Command
-Declares an interface for executing an operation.
ConcreteCommand
-Defines a binding between a Receiver object and an action
-Implements Execute by invoking the corresponding operation (s) on aggreger.
Client
-Create a ConcreteCommand ojbect and sets its receiver er.
Invoker
-Asks the command to carry out the request.
Cycler
-Konws how to perform the oprations associated with carrying out a request, Any class may serve as a cycler.

 


In Command mode, the sender and receiver of a Command are separated. The Command object is a bridge between the two.
First, you must define a Command interface that defines the operations supported by the Command, then define a specific Command class (ConcreteCommand), and specify the Command Receiver ), sometimes the receiver and command can be combined into one.
Using the polymorphism mechanism, the caller only needs to define the Command object. The details of the Command are transparent to the call.


Another important feature is undoable,
The undo process is the inverse process of Execute. The Memento mode is used for the more responsible revocation actions.


The command object is not necessarily a single command, but also a collection of many commands, so that the command does not need to define Invoker.


The sample code in Header First Design Patterns is as follows:

[Java]
Public interface Command {
Public void execute ();
Public void undo ();
}
 
Public class CeilingFanCommand implements Command {
Protected CeilingFan ceilingFan;
Protected int prevSpeed;
 
Public CeilingFanCommand (CeilingFan ceilingFan ){
This. ceilingFan = ceilingFan;
}
 
Public void execute (){
PrevSpeed = ceilingFan. getSpeed ();
}
 
Public void undo (){
If (prevSpeed = CeilingFan. HIGH ){
CeilingFan. high ();
} Else if (prevSpeed = CeilingFan. MEDIUM ){
CeilingFan. medium ();
} Else if (prevSpeed = CeilingFan. LOW ){
CeilingFan. low ();
} Else if (prevSpeed = CeilingFan. OFF ){
CeilingFan. off ();
}
}
}
Public class CeilingFanHighCommand extends CeilingFanCommand {
Public CeilingFanHighCommand (CeilingFan ceilingFan ){
Super (ceilingFan );
}
 
@ Override
Public void execute (){
Super.exe cute ();
CeilingFan. high ();
}
}
Public class CeilingFanMediumCommand extends CeilingFanCommand {
Public CeilingFanMediumCommand (CeilingFan ceilingFan ){
Super (ceilingFan );
}
 
@ Override
Public void execute (){
Super.exe cute ();
CeilingFan. medium ();
}
}
 
Public class CeilingFanOffCommand extends CeilingFanCommand {
Public CeilingFanOffCommand (CeilingFan ceilingFan ){
Super (ceilingFan );
}
 
@ Override
Public void execute (){
Super.exe cute ();
CeilingFan. low ();
}
}
 
Public class NoCommand implements Command {
Public NoCommand (){}

@ Override
Public void execute (){}
 
@ Override
Public void undo (){}
}
 
Public class CeilingFan {
Public static final int HIGH = 3;
Public static final int MEDIUM = 2;
Public static final int LOW = 1;
Public static final int OFF = 0;
String location;
Int speed;
 
Public CeilingFan (String location ){
This. location = location;
Speed = OFF;
}
 
Public void high (){
Speed = HIGH;
}
 
Public void medium (){
Speed = MEDIUM;
}
 
Public void low (){
Speed = LOW;
}
 
Public void off (){
Speed = OFF;
}
 
Public int getSpeed (){
Return speed;
}
}
 
Public class RemoteControlWithUndo {
Command [] onCommands;
Command [] offCommands;
Command undoCommand;
 
Public RemoteControlWithUndo (){
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;
}
UndoCommand = noCommand;
}
 
Public void setCommand (int slot, Command onCommand, Command offCommand ){
OnCommands [slot] = onCommand;
OffCommands [slot] = offCommand;
}
 
Public void onButtonWasPressed (int slot ){
OnCommands[slot].exe cute ();
UndoCommand = onCommands [slot];
}
 
Public void offButtonWasPressed (int slot ){
OffCommands[slot].exe cute ();
UndoCommand = offCommands [slot];
}
 
Public void undoButtonWasPressed (){
UndoCommand. undo ();
}
 
Public String toString (){
StringBuffer sb = new StringBuffer ();
Sb. append ("\ n ------ Remote Control ------ \ n ");
For (int I = 0; I <onCommands. length; I ++ ){
Sb. append ("[slot" + I + "]" + onCommands [I]. getClass (). getName () + "" + offCommands [I]. getClass (). getName () + "\ n ");
}
Return sb. toString ();
}
}
 
Public class RemoteLoader {
Public static void main (String [] args ){
RemoteControlWithUndo remoteControl = new RemoteControlWithUndo ();
 
CeilingFan ceilingFan = new CeilingFan ("Living Room ");
 
CeilingFanMediumCommand ceilingFanMedium =
New CeilingFanMediumCommand (ceilingFan );
CeilingFanHighCommand ceilingFanHigh =
New CeilingFanHighCommand (ceilingFan );
CeilingFanOffCommand ceilingFanOff =
New CeilingFanOffCommand (ceilingFan );
 
RemoteControl. setCommand (0, ceilingFanMedium, ceilingFanOff );
RemoteControl. setCommand (1, ceilingFanHigh, ceilingFanOff );
 
RemoteControl. onButtonWasPressed (0 );
RemoteControl. offButtonWasPressed (0 );
System. out. println (remoteControl );
RemoteControl. undoButtonWasPressed ();
 
RemoteControl. onButtonWasPressed (1 );
System. out. println (remoteControl );
RemoteControl. undoButtonWasPressed ();
}
}

Author: FeeLang

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.