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