Detailed description of command pattern

Source: Internet
Author: User

Command pattern


Address: http://blog.csdn.net/caroline_wendy


Command pattern): Encapsulate requests into objects so that different Request \ queue \ logs can be used to parameterize other objects.

The command mode also supports undo operations.


Implementation of the simple command mode:

1.Specific Class, Each class has a specific method:

/*** @ Time June 9, 2014 */package command;/*** @ author C. l. wang **/public class Light {public Light () {} public void on () {System. out. println ("Light is on");} public void off () {System. out. println ("Light is off") ;}}/*** @ time July 15/package command;/*** @ author C. l. wang **/public class GarageDoor {public GarageDoor () {} public void up () {System. out. println ("Garage Door is Open");} public void down () {System. out. println ("Garage Door is Closed");} public void stop () {System. out. println ("Garage Door is Stopped");} public void lightOn () {System. out. println ("Garage light is on");} public void lightOff () {System. out. println ("Garage light is off ");}}



2.Command INTERFACEProvides interfaces for all commands. The specific commands inherit the interface and the implementation method is as follows:

/*** @ Time June 9, 2014 */package command;/*** @ author C. L. Wang ***/public interface Command {public void execute ();}

2. Specific command implementationThat is, encapsulate the functions of a specific class into the command interface:

/*** @ Time June 9, 2014 */package command;/*** @ author C. l. wang **/public class LightOnCommand implements Command {Light light Light; public LightOnCommand (light Light light) {this. light = light;}/* (non-Javadoc) * @ see command. command # execute () */@ Overridepublic void execute () {// TODO Auto-generated method stublight. on () ;}}/***** @ time June 9, 2014 */package command;/***** @ author C. l. wang **/public class GarageDoorOpenCommand implements Command {GarageDoor garageDoor; public GarageDoorOpenCommand (GarageDoor garageDoor) {this. garageDoor = garageDoor;}/* (non-Javadoc) * @ see command. command # execute () */@ Overridepublic void execute () {// TODO Auto-generated method stubgarageDoor. up ();}}

3. Recipient, Execute the request, input command parameters, and execute the command in a unified manner:

/*** @ Time June 9, 2014 */package command;/*** @ author C. l. wang **/public class SimpleRemoteControl {Command slot; public SimpleRemoteControl () {}public void setCommand (Command command) {this. slot = command;} public void buttonWasPressed () using slot.exe cute ();}}

4. Execution class, Create a specific class, create a specific command, pass the specific command into the receiver class, and execute the Unified Action:

/*** @ Time June 9, 2014 */package command;/*** @ author C. l. wang **/public class RemoteControlTest {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stubSimpleRemoteControl remote = new SimpleRemoteControl (); Light light = new Light (); GarageDoor garageDoor = new GarageDoor (); lightOnCommand lightOn = new LightOnCommand (light); GarageDoorOpenCommand garageOpen = new GarageDoorOpenCommand (garageDoor); remote. setCommand (lightOn); remote. buttonWasPressed (); remote. setCommand (garageOpen); remote. buttonWasPressed ();}}

5. Output:

Light is onGarage Door is Open







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.