Software design - Lecture 43. The main topics covered in this chapter include: command design pattern or encapsulating invocation; motivation for command pattern; flow in the current design; violation of open/close principle; the necessary receiver objects;...
1 Software Design Lecture : 43 Command Design Pattern OR Encapsulating Invocation Example of Macro in Excel The Macro represents, at some extent, a command that is built from the reunion of a set of other commands, in a given order Motivation for Command Pattern In general, an objectoriented application consists of a set of interacting objects each offering limited, focused functionality In response to user interaction, the application carries out some kind of processing. For this purpose, the application makes use of the services of different objects for the processing requirement Motivation for Command Pattern In terms of implementation, the application may depend on a designated object that invokes methods on these objects by passing the required data as arguments INVOKER The designated object that invokes operations on different objects. The invoker may be treated as part of the client application Receiver The set of objects that actually contain the implementation to offer the services required for the request processing can be referred to as Receiver objects as shown in the next slide Flow in the Current Design In this design, the application that forwards the request and the set of Receiver objects that offer the services required to process the request are closely tied to each other in that they interact with each other directly. This could result in a set of conditional if statements in the implementation of the invoker as shown in next slide 10 if (RequestType=TypeA) { //do something } // Adding a new Type if (RequestType=TypeB) { //do something } 15 Working of Command Object Each Command object represents a different type of client request and the corresponding processing as shown in the next slide 16 17 Refinenement in Command Object A given Command object is responsible for offering the functionality required to process the request it represents, but it does not contain the actual implementation of the functionality. Command objects make use of Receiver objects in offering this functionality 18 Class Diagram 19 20 Working of Client: i It creates the necessary Receiver objects ii.It creates an appropriate Command object and configures it with the Receiver objects created in Step 1 iii.It creates an instance of the invoker and configures it with the Command object created in Step 2 iv.The invoker invokes the execute() method on the Command object v.As part of its implementation of the execute method, a typical Command object invokes necessary methods on the Receiver objects it contains to provide the required service to its caller 21 Command Pattern Defined “It encapsulates a request as an object thereby letting you parameterize other objects with different requests queue or log request and support undoable operations” 22 23 24 Flow of Application i Command object encapsulate a request by binding together a set of actions on a specific receiver by exposing a execute method ii When execute method is called it causes the actions to be invoked on a receiver iii From outside no other object know which action will be executed against a receiver iv They just know if they will call execute method their request will be served 25 Example 26 In stock exchange, the client creates some orders for buying and selling stocks (ConcreteCommands). Then the orders are sent to the agent (Invoker).The agent takes the orders and place them to the StockTrade system (Receiver). The agent keeps an internal queue with the order to be placed. Let's assume that the StockTrade system is closed each Monday, but the agent accepts orders, and queue them to be processed later on 27 28 To Do: Java Code 29 Hot Spots i The main advantage of the command design pattern is that it decouples the object that invokes the operation from the one that know how to perform it. And this advantage must be kept ii There are implementations of this design pattern in which the invoker is aware of the concrete commands classes. This is wrong making the implementation more tightly coupled. The invoker should be aware only about the abstract command class