It is behavioral pattern which is used solve the problem how two object interact using the principle of abstraction to achieve certain goal.In this pattern we have 4 main players client ,invoker , command and receiver .
Receiver : This Object has the actual algorithm to perform the task , which is abstracted from the outer world.
Command : This Object has the reference of the Receiver class which it executes method on the receiver .
Invoker :Is aware of the command to be executed , only executes the command on the command Object.
Client : Keeps invoker object and list of all the command objects and decides which command to execute when , client invokes the particular command using the invoker object.
So Switch is classic case of command design pattern ,which is invoker class and has the list of all the commands to be executed and we have command interface with a execute method now we can have two implementation of the Command interface switchON and switchOff which will call the Light Object On and Off method.Below is the sample implementation.
Receiver : This Object has the actual algorithm to perform the task , which is abstracted from the outer world.
Command : This Object has the reference of the Receiver class which it executes method on the receiver .
Invoker :Is aware of the command to be executed , only executes the command on the command Object.
Client : Keeps invoker object and list of all the command objects and decides which command to execute when , client invokes the particular command using the invoker object.
So Switch is classic case of command design pattern ,which is invoker class and has the list of all the commands to be executed and we have command interface with a execute method now we can have two implementation of the Command interface switchON and switchOff which will call the Light Object On and Off method.Below is the sample implementation.
import java.util.List; import java.util.ArrayList; /** The Command interface */ public interface Command { void execute(); } /** The Invoker class */ public class Switch { private List<Command> history = new ArrayList<Command>(); public void storeAndExecute(final Command cmd) { this.history.add(cmd); // optional cmd.execute(); } } /** The Receiver class */ public class Light { public void turnOn() { System.out.println("The light is on"); } public void turnOff() { System.out.println("The light is off"); } } /** The Command for turning on the light - ConcreteCommand #1 */ public class FlipUpCommand implements Command { private Light theLight; public FlipUpCommand(final Light light) { this.theLight = light; } @Override // Command public void execute() { theLight.turnOn(); } } /** The Command for turning off the light - ConcreteCommand #2 */ public class FlipDownCommand implements Command { private Light theLight; public FlipDownCommand(final Light light) { this.theLight = light; } @Override // Command public void execute() { theLight.turnOff(); } } /* The test class or client */ public class PressSwitch { public static void main(final String[] arguments){ // Check number of arguments if (arguments.length != 1) { System.err.println("Argument \"ON\" or \"OFF\" is required."); System.exit(-1); } final Light lamp = new Light(); final Command switchUp = new FlipUpCommand(lamp); final Command switchDown = new FlipDownCommand(lamp); final Switch mySwitch = new Switch(); switch(arguments[0]) { case "ON": mySwitch.storeAndExecute(switchUp); break; case "OFF": mySwitch.storeAndExecute(switchDown); break; default: System.err.println("Argument \"ON\" or \"OFF\" is required."); System.exit(-1); } } }
Comments
Post a Comment