Command Pattern
Enclose your request under an object as a command and pass it to the object which can invoke the appropriate behavior by using appropriate object which will handle the passed command and execute it.
Its an Action
Advantage
- It separates the invoker and the executor based on command operation.
- for example an invoker will invoke the relevant object for command execution.
- New command can be easily added.
It command pattern, have 4 actors.
- Command Request : Encapsulates command and its parameters.
- Receiver : who knows how to perform the requested action.
- Invoker : Sends the command request
- Client : create and configure the command and the invoker.
Usage
- When parametrized object is required as per required action
- creation and execution of request at different times
- when logging , transaction, rollback is required
- Its often used in a scenarios where you want to decouple the sender (client) of the request from the receiver (the object that performs the action) and provide a level of abstraction for invoking action
There are following participant in command design pattern
Command : an Interface for executing an operation.
ConcreteCommand : It extends the Command interface to execute the method. This class creates the binding between the action and the receiver.
Invoker : This class asks the command to carry out the request
Receiver : This class knows the operation.
Comments
Post a Comment