Skip to content

Commit 675b2f1

Browse files
committed
Update README.md
1 parent a4f2d14 commit 675b2f1

File tree

1 file changed

+51
-21
lines changed

1 file changed

+51
-21
lines changed

command/README.md

+51-21
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,34 @@ tags:
99
---
1010

1111
## Also known as
12+
1213
Action, Transaction
1314

1415
## Intent
15-
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
16+
17+
Encapsulate a request as an object, thereby letting you parameterize clients with different
18+
requests, queue or log requests, and support undoable operations.
1619

1720
## Explanation
1821
Real world example
1922

20-
> There is a wizard casting spells on a goblin. The spells are executed on the goblin one by one. The first spell shrinks the goblin and the second makes him invisible. Then the wizard reverses the spells one by one. Each spell here is a command object that can be undone.
23+
> There is a wizard casting spells on a goblin. The spells are executed on the goblin one by one.
24+
> The first spell shrinks the goblin and the second makes him invisible. Then the wizard reverses
25+
> the spells one by one. Each spell here is a command object that can be undone.
2126
2227
In plain words
2328

2429
> Storing requests as command objects allows performing an action or undoing it at a later time.
2530
2631
Wikipedia says
2732

28-
> 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.
33+
> In object-oriented programming, the command pattern is a behavioral design pattern in which an
34+
> object is used to encapsulate all information needed to perform an action or trigger an event at
35+
> a later time.
2936
3037
**Programmatic Example**
3138

32-
Here's the sample code with wizard and goblin. Let's start from the wizard class.
39+
Here's the sample code with wizard and goblin. Let's start from the `Wizard` class.
3340

3441
```java
3542
public class Wizard {
@@ -149,7 +156,7 @@ public class ShrinkSpell implements Command {
149156
}
150157
```
151158

152-
And last we have the goblin who's the target of the spells.
159+
Finally, we have the goblin who's the target of the spells.
153160

154161
```java
155162
public abstract class Target {
@@ -199,44 +206,67 @@ public class Goblin extends Target {
199206
}
200207
```
201208

202-
Finally here's the whole example in action.
209+
Here's the whole example in action.
203210

204211
```java
205212
var wizard = new Wizard();
206213
var goblin = new Goblin();
207214
goblin.printStatus();
208-
// Goblin, [size=normal] [visibility=visible]
209215
wizard.castSpell(new ShrinkSpell(), goblin);
210-
// Wizard casts Shrink spell at Goblin
211216
goblin.printStatus();
212-
// Goblin, [size=small] [visibility=visible]
213217
wizard.castSpell(new InvisibilitySpell(), goblin);
214-
// Wizard casts Invisibility spell at Goblin
215218
goblin.printStatus();
216-
// Goblin, [size=small] [visibility=invisible]
217219
wizard.undoLastSpell();
218-
// Wizard undoes Invisibility spell
219220
goblin.printStatus();
221+
```
222+
223+
Here's the program output:
224+
225+
```java
226+
// Goblin, [size=normal] [visibility=visible]
227+
// Wizard casts Shrink spell at Goblin
228+
// Goblin, [size=small] [visibility=visible]
229+
// Wizard casts Invisibility spell at Goblin
230+
// Goblin, [size=small] [visibility=invisible]
231+
// Wizard undoes Invisibility spell
220232
// Goblin, [size=small] [visibility=visible]
221233
```
222234

223235
## Class diagram
236+
224237
![alt text](./etc/command.png "Command")
225238

226239
## Applicability
227-
Use the Command pattern when you want to
228240

229-
* parameterize objects by an action to perform. 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. Commands are an object-oriented replacement for callbacks.
230-
* specify, queue, and execute requests at different times. A Command object can have a lifetime independent of the original request. 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
231-
* support undo. The Command's execute operation can store state for reversing its effects in the command itself. The Command interface must have an added Unexecute operation that reverses the effects of a previous call to execute. Executed commands are stored in a history list. Unlimited-level undo and redo is achieved by traversing this list backwards and forwards calling unexecute and execute, respectively
232-
* support logging changes so that they can be reapplied in case of a system crash. By augmenting the Command interface with load and store operations, you can keep a persistent log of changes. Recovering from a crash involves reloading logged commands from disk and re-executing them with the execute operation
233-
* structure a system around high-level operations build on primitive operations. Such a structure is common in information systems that support transactions. A transaction encapsulates a set of changes to data. The Command pattern offers a way to model transactions. Commands have a common interface, letting you invoke all transactions the same way. The pattern also makes it easy to extend the system with new transactions
241+
Use the Command pattern when you want to:
242+
243+
* Parameterize objects by an action to perform. You can express such parameterization in a
244+
procedural language with a callback function, that is, a function that's registered somewhere to be
245+
called at a later point. Commands are an object-oriented replacement for callbacks.
246+
* Specify, queue, and execute requests at different times. A Command object can have a lifetime
247+
independent of the original request. If the receiver of a request can be represented in an address
248+
space-independent way, then you can transfer a command object for the request to a different process
249+
and fulfill the request there.
250+
* Support undo. The Command's execute operation can store state for reversing its effects in the
251+
command itself. The Command interface must have an added un-execute operation that reverses the
252+
effects of a previous call to execute. The executed commands are stored in a history list.
253+
Unlimited-level undo and redo is achieved by traversing this list backwards and forwards calling
254+
un-execute and execute, respectively.
255+
* Support logging changes so that they can be reapplied in case of a system crash. By augmenting the
256+
Command interface with load and store operations, you can keep a persistent log of changes.
257+
Recovering from a crash involves reloading logged commands from disk and re-executing them with
258+
the execute operation.
259+
* Structure a system around high-level operations build on primitive operations. Such a structure is
260+
common in information systems that support transactions. A transaction encapsulates a set of changes
261+
to data. The Command pattern offers a way to model transactions. Commands have a common interface,
262+
letting you invoke all transactions the same way. The pattern also makes it easy to extend the
263+
system with new transactions.
234264

235265
## Typical Use Case
236266

237-
* to keep a history of requests
238-
* implement callback functionality
239-
* implement the undo functionality
267+
* To keep a history of requests
268+
* Implement callback functionality
269+
* Implement the undo functionality
240270

241271
## Real world examples
242272

0 commit comments

Comments
 (0)