-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
372 lines (368 loc) · 15.3 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>design-patterns</title>
<style>
body {
font-family: sans-serif;
margin: 8px;
}
header .main-title {
font-size: 1.5rem;
margin: 16px;
text-align: center;
}
main > section {
border-top: 1px solid grey;
}
</style>
</head>
<body>
<header>
<div class="main-title">
<a
href="https://github.com/scraggo/design-patterns-refactoring-guru"
rel="noopener"
target="_blank"
>design-patterns-refactoring-guru on github</a
>
</div>
</header>
<main>
<section class="suite">
<h1>Builder Pattern</h1>
<dl>
<section class="suite">
<h1>without Director</h1>
<dl>
<dt>ConcreteBuilder1 calls reset() after getProduct()</dt>
<dd><pre><code>builder.producePartA();
const product = builder.getProduct();
chai_1.expect(product).to.deep.equal({
parts: ['PartA1'],
});
chai_1.expect(builder.getProduct()).to.deep.equal({
parts: [],
});</code></pre></dd>
<dt>can build a custom project</dt>
<dd><pre><code>builder.producePartA();
builder.producePartC();
const product = builder.getProduct();
chai_1.expect(product).to.deep.equal({
parts: ['PartA1', 'PartC1'],
});
product.listParts();
chai_1.expect(this.logStub).to.have.been.calledOnceWithExactly('Product parts: PartA1, PartC1\n');</code></pre></dd>
</dl>
</section>
<section class="suite">
<h1>with Director</h1>
<dl>
<dt>builds Standard MVP</dt>
<dd><pre><code>director.buildMinimalViableProduct();
const product = builder.getProduct();
chai_1.expect(product).to.deep.equal({
parts: ['PartA1'],
});
product.listParts();
chai_1.expect(this.logStub).to.have.been.calledOnceWithExactly('Product parts: PartA1\n');</code></pre></dd>
<dt>builds Standard full featured product</dt>
<dd><pre><code>director.buildFullFeaturedProduct();
const product = builder.getProduct();
chai_1.expect(product).to.deep.equal({
parts: ['PartA1', 'PartB1', 'PartC1'],
});
product.listParts();
chai_1.expect(this.logStub).to.have.been.calledOnceWithExactly('Product parts: PartA1, PartB1, PartC1\n');</code></pre></dd>
</dl>
</section>
</dl>
</section>
<section class="suite">
<h1>index.ts</h1>
<dl>
<dt>runs default command</dt>
<dd><pre><code>if (context.timeout)
this.timeout(context.timeout);
return run.call(this);</code></pre></dd>
<dt>runs hello --name builder</dt>
<dd><pre><code>if (context.timeout)
this.timeout(context.timeout);
return run.call(this);</code></pre></dd>
</dl>
</section>
<section class="suite">
<h1>Iterator-Visitor Combination</h1>
<dl>
<section class="suite">
<h1>WordsDisplayComponent</h1>
<dl>
<dt>Component.display returns empty string if nothing to display</dt>
<dd><pre><code>this.component.display();
chai_1.expect(this.logStub).to.have.been.calledWith('');</code></pre></dd>
<dt>Component.display returns html string of words list</dt>
<dd><pre><code>this.component.accept(new iterator_visitor_1.HTMLDisplayVisitor());
this.component.display();
chai_1.expect(this.logStub).to.have.been.calledWith(`<html><body><ul>
<li>${wordsArr[0]}</li>
<li>${wordsArr[1]}</li>
<li>${wordsArr[2]}</li>
<li>${wordsArr[3]}</li>
</ul></body></html>`);</code></pre></dd>
<dt>Component.display returns html string of reversed words list</dt>
<dd><pre><code>this.component.accept(new iterator_visitor_1.HTMLReverseDisplayVisitor());
this.component.display();
chai_1.expect(this.logStub).to.have.been.calledWith(`<html><body><ul>
<li>${wordsArr[3]}</li>
<li>${wordsArr[2]}</li>
<li>${wordsArr[1]}</li>
<li>${wordsArr[0]}</li>
</ul></body></html>`);</code></pre></dd>
<dt>Component.display returns json string of words list</dt>
<dd><pre><code>this.component.accept(new iterator_visitor_1.JSONDisplayVisitor());
this.component.display();
chai_1.expect(this.logStub).to.have.been.calledWith(`{"items":["${wordsArr[0]}","${wordsArr[1]}","${wordsArr[2]}","${wordsArr[3]}"]}`);</code></pre></dd>
</dl>
</section>
</dl>
</section>
<section class="suite">
<h1>Iterator Pattern</h1>
<dl>
<section class="suite">
<h1>Collection</h1>
<dl>
<dt>getItems() works after addItem()</dt>
<dd><pre><code>chai_1.expect(this.words.getItems()).to.deep.equal([]);
addFour(this.words);
chai_1.expect(this.words.getItems()).to.deep.equal(wordsArr);</code></pre></dd>
<dt>getCount() works after addItems()</dt>
<dd><pre><code>chai_1.expect(this.words.getCount()).to.equal(0);
addFour(this.words);
chai_1.expect(this.words.getCount()).to.equal(4);</code></pre></dd>
</dl>
</section>
<section class="suite">
<h1>Iterator</h1>
<dl>
<dt>valid() is true before end of collection, false at end</dt>
<dd><pre><code>addFour(this.words);
const iterator = this.words.getIterator();
const collection = [];
while (iterator.valid()) {
iterator.next();
collection.push(iterator.valid());
}
chai_1.expect(collection).to.deep.equal([true, true, true, false]);</code></pre></dd>
<dt>valid() is true before end of collection, false at end for reverse iterator</dt>
<dd><pre><code>addFour(this.words);
const reverseIterator = this.words.getReverseIterator();
const collection = [];
while (reverseIterator.valid()) {
reverseIterator.next();
collection.push(reverseIterator.valid());
}
chai_1.expect(collection).to.deep.equal([true, true, true, false]);</code></pre></dd>
<dt>iterates in insertion order</dt>
<dd><pre><code>const iterator = this.words.getIterator();
let collection = [];
while (iterator.valid()) {
collection.push(iterator.next());
}
chai_1.expect(collection.length).to.equal(0);
addFour(this.words);
collection = [];
while (iterator.valid()) {
collection.push(iterator.next());
}
chai_1.expect(collection).to.deep.equal(wordsArr);</code></pre></dd>
<dt>iterates in reverse order</dt>
<dd><pre><code>addFour(this.words);
const reverseIterator = this.words.getReverseIterator();
const collection = [];
while (reverseIterator.valid()) {
collection.push(reverseIterator.next());
}
chai_1.expect(collection).to.deep.equal(reversedWordsArr);</code></pre></dd>
<dt>gets current item when current() is called</dt>
<dd><pre><code>addFour(this.words);
const iterator = this.words.getIterator();
chai_1.expect(iterator.current()).to.equal(wordsArr[0]);
iterator.next();
chai_1.expect(iterator.current()).to.equal(wordsArr[1]);
iterator.next();
chai_1.expect(iterator.current()).to.equal(wordsArr[2]);
iterator.next();
chai_1.expect(iterator.current()).to.equal(wordsArr[3]);</code></pre></dd>
<dt>gets key (index) when key() is called</dt>
<dd><pre><code>addFour(this.words);
const iterator = this.words.getIterator();
chai_1.expect(iterator.key()).to.equal(0);
iterator.next();
chai_1.expect(iterator.key()).to.equal(1);
iterator.next();
chai_1.expect(iterator.key()).to.equal(2);
iterator.next();
chai_1.expect(iterator.key()).to.equal(3);</code></pre></dd>
<dt>rewind() moves position to beginning of collection</dt>
<dd><pre><code>addFour(this.words);
const iterator = this.words.getIterator();
// move to end of collection
while (iterator.valid()) {
iterator.next();
}
chai_1.expect(iterator.valid()).to.be.false;
iterator.rewind();
chai_1.expect(iterator.current()).to.equal(wordsArr[0]);</code></pre></dd>
<dt>rewind() moves position to end of collection if reversed</dt>
<dd><pre><code>addFour(this.words);
const reverseIterator = this.words.getReverseIterator();
// move to beginning of collection
while (reverseIterator.valid()) {
reverseIterator.next();
}
chai_1.expect(reverseIterator.valid()).to.be.false;
reverseIterator.rewind();
chai_1.expect(reverseIterator.current()).to.equal(wordsArr[3]);</code></pre></dd>
</dl>
</section>
</dl>
</section>
<section class="suite">
<h1>Observer</h1>
<dl>
<section class="suite">
<h1>ConcreteSubject</h1>
<dl>
<dt>notify() calls no observers if none set</dt>
<dd><pre><code>this.subject.notify();
chai_1.expect(this.logStub).to.have.been.calledOnceWithExactly('Subject: Notifying observers...');</code></pre></dd>
<dt>attach() logs a failure if observer already attached</dt>
<dd><pre><code>this.subject.attach(this.observerA);
this.subject.attach(this.observerA);
chai_1.expect(this.logStub).to.have.callCount(2);
chai_1.expect(utils_test_1.getArgsForCall(this.logStub, 0)).to.deep.equal([
'Subject: Attached an observer.',
]);
chai_1.expect(utils_test_1.getArgsForCall(this.logStub, 1)).to.deep.equal([
'Subject: Observer has been attached already.',
]);</code></pre></dd>
<dt>notify() calls one observer if attached</dt>
<dd><pre><code>this.subject.attach(this.observerA);
this.subject.notify();
chai_1.expect(this.logStub).to.have.callCount(3);
chai_1.expect(utils_test_1.getArgsForCall(this.logStub, 0)).to.deep.equal([
'Subject: Attached an observer.',
]);
chai_1.expect(utils_test_1.getArgsForCall(this.logStub, 1)).to.deep.equal([
'Subject: Notifying observers...',
]);
chai_1.expect(utils_test_1.getArgsForCall(this.logStub, 2)).to.deep.equal([
'ConcreteObserverA: Reacted to the event.',
]);</code></pre></dd>
<dt>notify() calls two observers if attached</dt>
<dd><pre><code>this.subject.attach(this.observerA);
this.subject.attach(this.observerB);
this.subject.notify();
chai_1.expect(this.logStub).to.have.callCount(5);
chai_1.expect(utils_test_1.getArgsForCall(this.logStub, 0)).to.deep.equal([
'Subject: Attached an observer.',
]);
chai_1.expect(utils_test_1.getArgsForCall(this.logStub, 1)).to.deep.equal([
'Subject: Attached an observer.',
]);
chai_1.expect(utils_test_1.getArgsForCall(this.logStub, 2)).to.deep.equal([
'Subject: Notifying observers...',
]);
chai_1.expect(utils_test_1.getArgsForCall(this.logStub, 3)).to.deep.equal([
'ConcreteObserverA: Reacted to the event.',
]);
chai_1.expect(utils_test_1.getArgsForCall(this.logStub, 4)).to.deep.equal([
'ConcreteObserverB: Reacted to the event.',
]);</code></pre></dd>
<dt>Changes state and notifies on someBusinessLogic()</dt>
<dd><pre><code>this.subject.attach(this.observerA);
this.subject.someBusinessLogic();
chai_1.expect(this.logStub).to.have.callCount(4);
chai_1.expect(this.logStub).to.have.been.calledWith("\nSubject: I'm doing something important.");
chai_1.expect(this.logStub).to.have.been.calledWith('Subject: My state has just changed to: 11');</code></pre></dd>
<dt>Detaches an observer</dt>
<dd><pre><code>this.subject.attach(this.observerA);
this.subject.detach(this.observerA);
chai_1.expect(this.logStub).to.have.been.calledWith('Subject: Detached an observer.');</code></pre></dd>
<dt>Logs a failure message if a non-attached observer is tried to detach</dt>
<dd><pre><code>this.subject.attach(this.observerA);
this.subject.detach(this.observerB);
chai_1.expect(this.logStub).to.have.been.calledWith('Subject: Nonexistent observer.');</code></pre></dd>
</dl>
</section>
<section class="suite">
<h1>ConcreteObserverA</h1>
<dl>
<dt>logs on update() if component.state < 3</dt>
<dd><pre><code>this.subject.attach(this.observerA);
this.subject.notify();
chai_1.expect(this.logStub).to.have.callCount(3);
chai_1.expect(this.logStub).to.have.been.calledWith('ConcreteObserverA: Reacted to the event.');</code></pre></dd>
<dt>does not log on update() if component.state >= 3</dt>
<dd><pre><code>this.subject.attach(this.observerA);
this.subject.someBusinessLogic();
this.subject.notify();
chai_1.expect(this.logStub).not.to.have.been.calledWith('ConcreteObserverA: Reacted to the event.');</code></pre></dd>
</dl>
</section>
<section class="suite">
<h1>ConcreteObserverB</h1>
<dl>
<dt>logs on update() if component.state === 0</dt>
<dd><pre><code>this.subject.attach(this.observerB);
this.subject.notify();
chai_1.expect(this.logStub).to.have.callCount(3);
chai_1.expect(this.logStub).to.have.been.calledWith('ConcreteObserverB: Reacted to the event.');</code></pre></dd>
<dt>logs on update() if component.state >=2</dt>
<dd><pre><code>this.subject.attach(this.observerB);
this.subject.someBusinessLogic();
this.subject.notify();
chai_1.expect(this.logStub).to.have.callCount(7);
chai_1.expect(this.logStub).to.have.been.calledWith('ConcreteObserverB: Reacted to the event.');</code></pre></dd>
<dt>does not log on update() if component.state === 1</dt>
<dd><pre><code>this.mathRandomStub.returns(1 / 11);
this.subject.attach(this.observerB);
this.subject.someBusinessLogic();
this.subject.notify();
chai_1.expect(this.logStub).to.have.callCount(5);
chai_1.expect(this.logStub).not.to.have.been.calledWith('ConcreteObserverB: Reacted to the event.');</code></pre></dd>
</dl>
</section>
</dl>
</section>
<section class="suite">
<h1>Visitor Pattern</h1>
<dl>
<dt>acceptVisitorForAll() accepts visitor1 for both components</dt>
<dd><pre><code>const visitor1 = new visitor_1.ConcreteVisitor1();
visitor_1.acceptVisitorForAll(components, visitor1);
chai_1.expect(this.logStub).to.have.been.calledTwice;
chai_1.expect(utils_test_1.getArgsForCall(this.logStub, 0)).to.deep.equal([
'A + ConcreteVisitor1',
]);
chai_1.expect(utils_test_1.getArgsForCall(this.logStub, 1)).to.deep.equal([
'B + ConcreteVisitor1',
]);</code></pre></dd>
<dt>acceptVisitorForAll() accepts visitor2 for both components</dt>
<dd><pre><code>const visitor2 = new visitor_1.ConcreteVisitor2();
visitor_1.acceptVisitorForAll(components, visitor2);
chai_1.expect(this.logStub).to.have.been.calledTwice;
chai_1.expect(utils_test_1.getArgsForCall(this.logStub, 0)).to.deep.equal([
'A + ConcreteVisitor2',
]);
chai_1.expect(utils_test_1.getArgsForCall(this.logStub, 1)).to.deep.equal([
'B + ConcreteVisitor2',
]);</code></pre></dd>
</dl>
</section>
</main>
</body>
</html>