You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
> In object-oriented programming, the command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time. This information includes the method name, the object that owns the method and values for the method parameters.
78
+
79
+
<br/>
80
+
81
+
* In plain words:
82
+
83
+
* Enable the construction of components that are able to delegate, sequence or execute method calls at a time of their choosing;
84
+
85
+
---
86
+
87
+
# Explanation
88
+
89
+
Four terms always associated with the pattern:
90
+
91
+
* Command
92
+
* Object that knows about the receiver and invokes a method of the receiver;
93
+
94
+
* Receiver
95
+
* Object that does the work;
96
+
97
+
* Invoker
98
+
* Knows how to execute a command, and optionally does the bookkeeping about the command execution;
99
+
100
+
* Client
101
+
* Decides which commands to execute at which points, passing the command object to the invoker object;
102
+
103
+
---
104
+
105
+
# Example
106
+
107
+
In our example, a Wizard can cast spells on targets.
108
+
109
+
* Spell will be the command (implements the Command interface);
* You can express such parameterization in a procedural language with a callback function, that is, a function that's registered somewhere to be called at a later point;
151
+
152
+
* Commands are an object-oriented replacement for callbacks;
153
+
154
+
<br/>
155
+
156
+
* Specify, queue, and execute requests at different times;
157
+
* A Command object can have a lifetime independent of the original request;
158
+
159
+
* If the receiver of a request can be represented in an address space-independent way, then you can transfer a command object for the request to a different process and fulfill the request there;
160
+
161
+
---
162
+
163
+
# Applicability
164
+
165
+
Use the Command pattern when you want to:
166
+
167
+
* Support undo;
168
+
* The Command's execute() operation can store state for reversing its effects in the command itself;
169
+
170
+
* The Command interface must have an added unexecute() operation that reverses the effects of a previous call to execute;
171
+
172
+
* Executed commands are stored in a history list;
173
+
174
+
* Unlimited-level undo and redo is achieved by traversing this list backwards and forwards calling unexecute() and execute(), respectively;
175
+
176
+
---
177
+
178
+
# Applicability
179
+
180
+
Use the Command pattern when you want to:
181
+
182
+
* Support logging changes so that they can be reapplied in case of a system crash;
183
+
* By augmenting the Command interface with load() and store() operations, you can keep a persistent log of changes;
184
+
185
+
* Recovering from a crash involves reloading logged commands from disk and re-executing them with the execute operation;
186
+
187
+
---
188
+
189
+
# Applicability
190
+
191
+
Use the Command pattern when you want to:
192
+
193
+
* Structure a system around high-level operations build on primitive operations;
194
+
* Such a structure is common in information systems that support transactions;
195
+
196
+
* A transaction encapsulates a set of changes to data;
197
+
198
+
* The Command pattern offers a way to model transactions;
199
+
200
+
* Commands have a common interface, letting you invoke all transactions the same way;
201
+
202
+
* The pattern also makes it easy to extend the system with new transactions
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)
0 commit comments