What do command objects do




















Data Mining. Data Warehouse. Javatpoint Services JavaTpoint offers too many high quality services. It is also known as Action or Transaction. Advantage of command pattern It separates the object that invokes the operation from the object that actually performs the operation.

It makes easy to add new commands, because existing classes remain unchanged. Usage of command pattern: It is used: When you need parameterize objects according to an action perform. When you need to create and execute requests at different times.

When you need to support rollback, logging or transaction functionality. Example of command pattern Let's understand the example of adapter design pattern by the above UML diagram. UML for command pattern: These are the following participants of the Command Design pattern: Command This is an interface for executing an operation. ConcreteCommand This class extends the Command interface and implements the execute method.

This class creates a binding between the action and the receiver. Client This class creates the ConcreteCommand class and associates it with the receiver.

Invoker This class asks the command to carry out the request. Receiver This class knows to perform the operation. File: Document. File: ActionOpen. A Command class holds some subset of the following: an object, a method to be applied to the object, and the arguments to be passed when the method is applied. The Command's "execute" method then causes the pieces to come together. The client that creates a command is not the same client that executes it. This separation provides flexibility in the timing and sequencing of commands.

Materializing commands as objects means they can be passed, staged, shared, loaded in a table, and otherwise instrumented or manipulated like any other object. Command objects can be thought of as "tokens" that are created by one client that knows what need to be done, and passed to another client that has the resources for doing it.

The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parametrized with different requests. Suppose you are building a home automation system. There is a programmable remote which can be used to turn on and off various items in your home like lights, stereo, AC etc. It looks something like this. Also we can reassign a button to do something else.

By using simple if-else we are coding to implementation rather than interface. Also there is tight coupling. So what we want to achieve is a design that provides loose coupling and remote control should not have much information about a particular device. The command pattern helps us do that. Definition: The command pattern encapsulates a request as an object, thereby letting us parameterize other objects with different requests, queue or log requests, and support undoable operations.

In analogy to our problem above remote control is the client and stereo, lights etc. In command pattern there is a Command object that encapsulates a request by binding together a set of actions on a specific receiver. It does so by exposing just one method execute that causes some actions to be invoked on the receiver.

Parameterizing other objects with different requests in our analogy means that the button used to turn on the lights can later be used to turn on stereo or maybe open the garage door. The Command may have an added unExecute operation that reverses the effects of a previous call to execute. It may also support logging changes so that they can be reapplied in case of a system crash.



0コメント

  • 1000 / 1000