Please enable JavaScript.
Coggle requires JavaScript to display documents.
COMMAND PATTERN, Receiver
Action() - Coggle Diagram
COMMAND PATTERN
DEFINITION
Encapsulates a request as an object thereby letting you parametrize other objects with different requests queues or log requests and support undoable operations
Encapsulates the request
- We have an object
- Somebody sends a request to that object to do something
- That request is what we are trying to encapsulate
Letting you parametrize other objects with different requests queues or log requests
- We can then take these encapsulated commands and compose them
- We have some commands, put them in a list and invoke them
- With composite pattern we can have a command which contain multiple commands
Support undoable operations
- I have a command that do something
- I have a command that undo something
- Undoing becomes super easy because every command has an unexecute method
- We can make macro commands
Photoshop Example
- Every action we perform on the picture is a command
- We can stack all the command in a list and we can unexecute the command and pop it out of the list
CONCEPT
- Instead of just performing some action. We wrap the action we want to perform in a particular command and we make sure that is undoable
UML
-
-
WORKFLOW
- The invoker is loaded with a particular command
- The concrete implementation is a command
- Whenever the command is executed it performs some particular action on the receiver
CODE
-
class Invoker
ICommand On;
ICommand Off;
ICommand Down;
ICommand Up;
// Constructor
public Invoker(commands)...
// Buttons
public void ClickOn()
{this.On.Execute();}
- The invoker can have anything, different classes with different interfaces
- The notion is simple we need to be able to load an invoker with commands
AVOID MUTATION
- It's better to pass the commands to the constructor, because the invoker necessary needs 4 commands
-
-
-
SCENARIO
We are running a company and we have to build an app to program a remote control device for controlling lights
We want to have a set of commands and attach it to a particular button of the remote control (Invoker)
- We press a button and send the command to a particular device
- Whenever the invoker is pressed we execute the command, we send it to a particular object (receiver)
-