Skip to content

Commit 4dbf646

Browse files
committed
Add Command presentation
1 parent 506d316 commit 4dbf646

File tree

2 files changed

+243
-0
lines changed

2 files changed

+243
-0
lines changed

command/etc/diagram.png

56.2 KB
Loading

command/etc/presentation.html

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
<!--
2+
3+
The MIT License
4+
Copyright (c) 2017 Rodolfo Forte
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
24+
-->
25+
<!DOCTYPE html>
26+
<html>
27+
<head>
28+
<title>Design Patterns - Command Presentation</title>
29+
<meta charset="utf-8">
30+
<style>
31+
@import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
32+
@import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic);
33+
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);
34+
35+
body { font-family: 'Droid Serif'; }
36+
h1, h2, h3 {
37+
font-family: 'Yanone Kaffeesatz';
38+
font-weight: normal;
39+
}
40+
.remark-code, .remark-inline-code { font-family: 'Ubuntu Mono'; }
41+
42+
blockquote {
43+
border-left: 0.3em solid rgba(0,0,0,0.5);
44+
padding: 0 15px;
45+
font-style: italic;
46+
}
47+
48+
img {
49+
max-width:100%;
50+
}
51+
</style>
52+
</head>
53+
<body>
54+
<textarea id="source">
55+
56+
class: center, middle
57+
58+
# Command
59+
60+
---
61+
62+
# Also known as
63+
64+
* Action, Transaction
65+
66+
---
67+
68+
# Intent
69+
70+
* Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
71+
72+
---
73+
74+
# Explanation
75+
76+
* [Wikipedia](https://en.wikipedia.org/wiki/Command_pattern) says:
77+
> 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);
110+
* Goblin (a Target object) will be the receiver;
111+
* Wizard will be the invoker;
112+
* App will be the client;
113+
114+
```
115+
116+
//App.java
117+
public static void main(String[] args) {
118+
Wizard wizard = new Wizard();
119+
Goblin goblin = new Goblin();
120+
goblin.printStatus(); //Goblin, [size=normal] [visibility=visible]
121+
122+
wizard.castSpell(new ShrinkSpell(), goblin);
123+
wizard.castSpell(new InvisibilitySpell(), goblin);
124+
125+
goblin.printStatus(); //Goblin, [size=small] [visibility=invisible]
126+
127+
wizard.undoLastSpell();
128+
129+
goblin.printStatus(); //Goblin, [size=small] [visibility=visible]
130+
131+
wizard.redoLastSpell();
132+
goblin.printStatus(); //Goblin, [size=small] [visibility=invisible]
133+
}
134+
```
135+
136+
---
137+
138+
# Diagram
139+
140+
.center[![Diagram](diagram.png)]
141+
142+
143+
---
144+
145+
# Applicability
146+
147+
Use the Command pattern when you want to:
148+
149+
* Parameterize objects by an action to perform;
150+
* 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
203+
204+
---
205+
206+
# Use Cases
207+
208+
* To keep a history of requests;
209+
210+
* Implement callback functionality;
211+
212+
* Implement the undo functionality;
213+
214+
---
215+
216+
# Real world examples
217+
218+
[java.lang.Runnable](http://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html)
219+
220+
[Netflix Hystrix](https://github.com/Netflix/Hystrix/wiki)
221+
222+
[javax.swing.Action](http://docs.oracle.com/javase/8/docs/api/javax/swing/Action.html)
223+
224+
---
225+
226+
# Credits
227+
228+
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)
229+
230+
---
231+
232+
# Tutorials
233+
234+
* Source code http://java-design-patterns.com/patterns/command/
235+
236+
</textarea>
237+
<script src="https://gnab.github.io/remark/downloads/remark-latest.min.js">
238+
</script>
239+
<script>
240+
var slideshow = remark.create();
241+
</script>
242+
</body>
243+
</html>

0 commit comments

Comments
 (0)