Command Design Pattern
Question : - What is Command Design Pattern?
This pattern falls under behavioural pattern. All the information required to invoke a method is encapsulated in this pattern. One of the most popular framework “Struts” uses this design pattern. This pattern helps us to build our command objects into a “ready to run” state and also help to pass those objects to an "invoker" class which makes a callback to the execute() method of each class at the exact/appropriate time. This invoker object does not know about the logic which a command object contains, it only knows that it has to call an execute() method when an event occurs.
As per GOF :-“Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”
Question : - When to use Command design pattern?
1. Performing action to process the complete workflow of request and response.
- Thread Pools – A thread pool class is having a method which adds a work item to an internal queue of tasks and these tasks will wait to be completed. It maintains a pool of threads that execute commands from the queue. The items available in the queue are known as command objects.
- For implementing Graphical User Interface menu items and buttons.
- In networking for playing online games – It might be require to send complete command objects across the network to be executed on multiple nodes.
- This pattern is also useful for wizards, progress bars, parallel processing e.t.c.
Advantage of using command design pattern : -
- This pattern helps to decouple the invoker and receiver implementation. Receiver knows how to process the request.
- Client creats a concrete command object and specifies to its receiver.
- An invoker object stores the concrete command object. Then the invoker issues a request by calling execute() method on the command object.
- The concrete command object invoke operations on its receiver to carry out the request.
- Command implementation selects the method to invoke on receiver object, and it works as a bridge between receiver and action methods.
- This(Command) pattern is easily extensible, we can add multiple action methods in receivers and create new Command implementations without changing the client code.
- The disadvantage or drawback with this pattern is that the code gets huge and it is confusing with high number of action methods and their associations.
- Fundamental terms of Command design pattern is Client, Invoker and Receiver.
In Jdk
Runnable interface (java.lang.Runnable) and Swing Action (javax.swing.Action) uses command design pattern.
In Live example
we can think about the order placed online in any shopping site, where it executes for appropriate actions for calling chosen payment gateway for money deduction and confirming the client order with selected items.
In the below example we will see how to implement this design pattern in Java programming.
Command .java
package com.gaurav.designpattern.command;
/**
* @author gaurav
*
*/
public interface Command {
public void execute();
}
RemoteControl.java
package com.gaurav.designpattern.command;
/**
* @author gaurav
*
*/
public class RemoteControl {
private Command putOn;
private Command putOff;
public RemoteControl(Command on, Command off) {
putOn = on;
putOff = off;
}
public void startOn() {
putOn.execute();
}
public void startOff() {
putOff.execute();
}
}
AirCondition.java
package com.gaurav.designpattern.command;
/**
* @author gaurav
*
*/
public class AirCondition {
public void startCooling() {
System.out.println("Air condition is started cooling.");
}
public void stopCooling() {
System.out.println("Air condition is off now.");
}
}
LedTelevision.java
package com.gaurav.designpattern.command;
/**
* @author gaurav
*
*/
public class LedTelevision {
public void turnOn() {
System.out.println("TV is on now.");
}
public void turnOff() {
System.out.println("TV is off.");
}
}
StartCoolingCommand.java
package com.gaurav.designpattern.command;
/**
* @author gaurav
*
*/
public class StartCoolingCommand implements Command {
private AirCondition airCondition;
public StartCoolingCommand(AirCondition ac) {
airCondition = ac;
}
@Override
public void execute() {
airCondition.startCooling();
}
}
StopCoolingCommand.java
package com.gaurav.designpattern.command;
/**
* @author gaurav
*
*/
public class StopCoolingCommand implements Command {
private AirCondition airCondition;
public StopCoolingCommand(AirCondition ac) {
airCondition = ac;
}
@Override
public void execute() {
airCondition.stopCooling();
}
}
TurnOnCommand.java
package com.gaurav.designpattern.command;
/**
* @author gaurav
*
*/
public class TurnOnCommand implements Command {
private LedTelevision ledTelevision;
public TurnOnCommand(LedTelevision led) {
ledTelevision = led;
}
@Override
public void execute() {
ledTelevision.turnOn();
}
}
TurnOffCommand.java
package com.gaurav.designpattern.command;
/**
* @author gaurav
*
*/
public class TurnOffCommand implements Command {
private LedTelevision ledTelevision;
public TurnOffCommand(LedTelevision led) {
ledTelevision = led;
}
@Override
public void execute() {
ledTelevision.turnOff();
}
}
CommandDesignPatternDemo.java
package com.gaurav.designpattern.command;
/**
* @author gaurav
*
*/
public class CommandDesignPatternDemo {
public static void main(String args[]) {
AirCondition airCondition = new AirCondition();
StartCoolingCommand startCC = new StartCoolingCommand(airCondition);
StopCoolingCommand stopCC = new StopCoolingCommand(airCondition);
RemoteControl acRemoteControl = new RemoteControl(startCC, stopCC);
acRemoteControl.startOn();
acRemoteControl.startOff();
LedTelevision ledTV = new LedTelevision();
TurnOnCommand turnOnCommand = new TurnOnCommand(ledTV);
TurnOffCommand turnOffCommand = new TurnOffCommand(ledTV);
RemoteControl tvRemoteControl = new RemoteControl(turnOnCommand,
turnOffCommand);
tvRemoteControl.startOn();
tvRemoteControl.startOff();
}
}
Result:-
Air condition is started cooling
Air condition is off now
TV is on now.
TV is off.
Execution Flow:-
No comments:
Post a Comment