Command Design Pattern : Command design pattern comes under behavioral design pattern. Command design patter is used to implement request-response model.
To understand command pattern in a better way, let’s understand key components first:
Command: A command is an interface which declares execute() method to execute a specific action.
Concrete Command : This class is an implementation of Command interface which provides actual implementation of execute() method.
Invoker : An invoker class contains a reference of a command and instructs the command to perform an action.
Receiver : A Receiver is a class which provides logic to perform actions asked by command.Execute() method of concrete command class invokes action method of receiver class.
Client : Client class will create instances of Receiver, Invoker and concrete command and then associates ConcreteCommand with Receiver class to perform action.
In command design pattern, client class creates instance of concrete command and set receiver object in concrete command. After that it creates instance of Invoker class and set command object in it and then invoker class forwards request from client to command object .Invoker has no knowledge about action performed by receiver class it just invokes command which then executes the action of receiver class.
Client Class: CommandDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.j2eereference.designPattern; public class CommandDemo { public static void main(String args[]) { //Receiver Class AdditionReceiver additionReceiver = new AdditionReceiver(); //Concrete command class Command addCommand = new AdditionCommand(additionReceiver); //Invoker class ActionInvoker actionInvoker = new ActionInvoker(addCommand); actionInvoker.invokeMethod(); } } |
Invoker class: ActionInvoker.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.j2eereference.designPattern; public class ActionInvoker { Command command; public ActionInvoker(Command command) { this.command = command; } public void setCommand(Command command) { this.command = command; } public void invokeMethod() { command.execute(); } } |
Command Interface: Command.java
1 2 3 4 5 |
package com.j2eereference.designPattern; public interface Command { public void execute(); } |
Concrete command class: AdditionCommand.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.j2eereference.designPattern; public class AdditionCommand implements Command { AdditionReceiver additionReceiver; public AdditionCommand(AdditionReceiver additionReceiver) { this.additionReceiver = additionReceiver; } @Override public void execute() { // TODO Auto-generated method stub System.out.println("Request for addition operation has been raised"); additionReceiver.additionAction(); } } |
Receiver Class: AdditionReceiver
1 2 3 4 5 6 7 8 9 |
package com.j2eereference.designPattern public class AdditionReceiver { public void additionAction() { System.out.println("Performing Addition operation"); } } |
OutPut:
Request for addition operation has been raised
Performing Addition operation
RealTime Use Cases :
- Implementation of Runnable , callable follows command design pattern
- In Real life, when you place order in restaurant then you are client who gives command to waiter, waiter then contact the chef to prepare your order.
- Remote control is an other good example of command design pattern as when you press On button then TV starts up and when you press Off button the it accept your request and closes the TV.
[…] Command Design Pattern : http://209.97.166.197/command-design-pattern/ […]