Command Pattern Prepared by Amol Vaze Command Pattern

Command Pattern Prepared by Amol Vaze
![Command Pattern �Command Pattern is used to encapsulate a request as an object[command] & Command Pattern �Command Pattern is used to encapsulate a request as an object[command] &](http://slidetodoc.com/presentation_image_h2/3f6af961e11c72344fe661c884efbc7e/image-2.jpg)
Command Pattern �Command Pattern is used to encapsulate a request as an object[command] & pass to an invoker. �Command pattern is behavioural design pattern. �Invoker is unaware of how to service requests uses the encapsulated command to perform the action.



Command Pattern- Hotel Example � Command Pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests. � The “Check” at a dinner is an example of a command pattern. � In example, waiter takes an order from a customer & encapsulates it by writing it on the check. � The order is queued for a shorter order cook. � Check pad is being used by waiter is independent of the menu & hence it supports commands to cook many different items.

Command Pattern – Menu Example � In menu example, we have menu options as invoker object which does not know how to handle request. � Invoker object use the command object execute method to perform the needed menu operations. � Also we have our document as receiver object. � Execute method calls receiver object to handle the request. � Command objects in this example have following 3 commands: Ø Open Command Ø Close Command Ø Save Command


Command Pattern- Class Diagram

Command – Sequence Diagram

Command – Objects Creation & Flow

Command Pattern – Implementation

Command Pattern – Code We have following java classes for our Menu real life example. Ø Word. Document. java Ø Command. java Ø Open. Command. java Ø Save. Command. java Ø Close. Command. java Ø Menu. Options. java Ø Client. java

Word. Docment. Java code This class serves as Receiver in our working example. public class Word. Document { public void open() { System. out. println("Document Opened"); } public void save() { System. out. println("Document Saved"); } public void close() { System. out. println("Document Closed"); } }

Command Interface Code This is a command interface in our working example which has a method called “execute()”. Command. java public interface Command { public void execute(); }

Open. Command. Java code This class is responsible for opening document and since it implements Command interface it has to override implemented “execute” method. public class Open. Command implements Command { private Word. Document word. Document; public Open. Command( Word. Document word. Document ) { this. word. Document = word. Document; } @Override public void execute() { word. Document. open(); } }

Save. Command. Java code This class is responsible for saving document and since it implements Command interface it has to override implemented “execute” method. public class Save. Command implements Command { private Word. Document word. Document; public Save. Command( Word. Document word. Document ) { this. word. Document = word. Document; } @Override public void execute() { word. Document. save(); } }

Close. Command. Java code This class is responsible for closing document and since it implements Command interface it has to override implemented “execute” method. public class Close. Command implements Command { private Word. Document word. Document; public Close. Command( Word. Document word. Document ) { this. word. Document = word. Document; } @Override public void execute() { word. Document. close(); } }

Menu. Options. Java Code public class Menu. Options { private Command open. Command; private Command save. Command; private Command close. Command; public Menu. Options( Command open, Command save, Command close ) { this. open. Command = open; this. save. Command = save; this. close. Command = close; } public void click. Open() { open. Command. execute(); } public void click. Save() { save. Command. execute(); } public void click. Close() { close. Command. execute(); } }

Client. Java Code This is main client class which has objects of all other classes and it invokes respective class methods to perform the actions. public class Client { public static void main( String[] args ) { Word. Document word. Document = new Word. Document(); Command open. Command = new Open. Command(word. Document); Command save. Command = new Save. Command(word. Document); Command close. Command = new Close. Command(word. Document); Menu. Options menu = new Menu. Options(open. Command, save. Command, close. Command); menu. click. Open(); menu. click. Save(); menu. click. Close(); } }

Output For Menu Code Hence, finally this java code is able to produce the following output using Command design pattern. Output Document Opened Document Saved Document Closed

Thank You!
- Slides: 21