Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Command Pattern Prepared by Amol Vaze 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. WordDocument.java Command.java OpenCommand.java SaveCommand.java CloseCommand.java MenuOptions.java Client.java WordDocment. Java code This class serves as Receiver in our working example. public class WordDocument { 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(); } OpenCommand.Java code This class is responsible for opening document and since it implements Command interface it has to override implemented “execute” method. public class OpenCommand implements Command { private WordDocument wordDocument; public OpenCommand( WordDocument wordDocument ) { this.wordDocument = wordDocument; } @Override public void execute() { wordDocument.open(); } } SaveCommand.Java code This class is responsible for saving document and since it implements Command interface it has to override implemented “execute” method. public class SaveCommand implements Command { private WordDocument wordDocument; public SaveCommand( WordDocument wordDocument ) { this.wordDocument = wordDocument; } @Override public void execute() { wordDocument.save(); } } CloseCommand.Java code This class is responsible for closing document and since it implements Command interface it has to override implemented “execute” method. public class CloseCommand implements Command { private WordDocument wordDocument; public CloseCommand( WordDocument wordDocument ) { this.wordDocument = wordDocument; } @Override public void execute() { wordDocument.close(); } } MenuOptions. Java Code public class MenuOptions { private Command openCommand; private Command saveCommand; private Command closeCommand; public MenuOptions( Command open, Command save, Command close ) { this.openCommand = open; this.saveCommand = save; this.closeCommand = close; } public void clickOpen() { openCommand.execute(); } public void clickSave() { saveCommand.execute(); } public void clickClose() { closeCommand.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 ) { WordDocument wordDocument = new WordDocument(); Command openCommand = new OpenCommand(wordDocument); Command saveCommand = new SaveCommand(wordDocument); Command closeCommand = new CloseCommand(wordDocument); MenuOptions menu = new MenuOptions(openCommand, saveCommand, closeCommand); menu.clickOpen(); menu.clickSave(); menu.clickClose(); } } 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!