@@ -3,8 +3,10 @@ const patterns = [
3
3
* Abstract
4
4
*/
5
5
{
6
+ id : 'abstract_factory' ,
6
7
name : 'Abstract Factory' ,
7
8
type : 'creational' ,
9
+ hint : 'groups object factories that have a common theme' ,
8
10
codeES5 : `function droidProducer(kind) {
9
11
if (kind === 'battle') return battleDroidPattern;
10
12
return pilotDroidPattern;
@@ -60,8 +62,10 @@ export default droidProducer;`
60
62
* Builder
61
63
*/
62
64
{
65
+ id : 'builder' ,
63
66
name : 'Builder' ,
64
67
type : 'creational' ,
68
+ hint : 'constructs complex objects by separating construction and representation' ,
65
69
codeES5 : `function Request() {
66
70
this.url = '';
67
71
this.method = '';
@@ -131,8 +135,10 @@ export default RequestPattern;`
131
135
* Factory
132
136
*/
133
137
{
138
+ id : 'factory' ,
134
139
name : 'Factory' ,
135
140
type : 'creational' ,
141
+ hint : 'creates objects without specifying the exact class to create' ,
136
142
codeES5 : `function teslaPattern(type) {
137
143
if (type === 'ModelX') return new Tesla(type, 108000, 300);
138
144
if (type === 'ModelS') return new Tesla(type, 111000, 320);
@@ -166,8 +172,10 @@ export default TeslaPattern;`
166
172
* Prototype
167
173
*/
168
174
{
175
+ id : 'prototype' ,
169
176
name : 'Prototype' ,
170
177
type : 'creational' ,
178
+ hint : 'creates objects by cloning an existing object' ,
171
179
codeES5 : `function Sheep(name, weight) {
172
180
this.name = name;
173
181
this.weight = weight;
@@ -195,8 +203,10 @@ export default Sheep;`
195
203
* Singleton
196
204
*/
197
205
{
206
+ id : 'singleton' ,
198
207
name : 'Singleton' ,
199
208
type : 'creational' ,
209
+ hint : 'restricts object creation for a class to only one instance' ,
200
210
codeES5 : `function Person() {
201
211
if (typeof Person.instance === 'object') return Person.instance;
202
212
@@ -222,8 +232,11 @@ export default Person;`
222
232
* Adapter
223
233
*/
224
234
{
235
+ id : 'adapter' ,
225
236
name : 'Adapter' ,
226
237
type : 'structural' ,
238
+ hint :
239
+ 'allows classes with incompatible interfaces to work together by wrapping its own interface around that of an already existing class' ,
227
240
codeES5 : `function Soldier(lvl) {
228
241
this.lvl = lvl;
229
242
}
@@ -285,8 +298,10 @@ export { Soldier, Jedi, JediPattern };`
285
298
* Bridge
286
299
*/
287
300
{
301
+ id : 'bridge' ,
288
302
name : 'Bridge' ,
289
303
type : 'structural' ,
304
+ hint : 'decouples an abstraction from its implementation so that the two can vary independently' ,
290
305
codeES5 : `function EpsonPrinter(ink) {
291
306
this.ink = ink();
292
307
}
@@ -363,8 +378,10 @@ export { EpsonPrinter, HPprinter, AcrylicInk, AlcoholInk };`
363
378
* Composite
364
379
*/
365
380
{
381
+ id : 'composite' ,
366
382
name : 'Composite' ,
367
383
type : 'structural' ,
384
+ hint : 'composes zero-or-more similar objects so that they can be manipulated as one object' ,
368
385
codeES5 : `function EquipmentPattern(name) {
369
386
this.equipments = [];
370
387
this.name = name;
@@ -484,8 +501,10 @@ export { Cabbinet, FloppyDisk, HardDrive, Memory };`
484
501
* Decorator
485
502
*/
486
503
{
504
+ id : 'decorator' ,
487
505
name : 'Decorator' ,
488
506
type : 'structural' ,
507
+ hint : 'dynamically adds/overrides behaviour in an existing method of an object' ,
489
508
codeES5 : `function Pasta() {
490
509
this.price = 0;
491
510
}
@@ -568,8 +587,10 @@ export { Penne, SaucePattern, CheesePattern };`
568
587
* Facade
569
588
*/
570
589
{
590
+ id : 'facade' ,
571
591
name : 'Facade' ,
572
592
type : 'structural' ,
593
+ hint : 'provides a simplified interface to a large body of code' ,
573
594
codeES5 : `var shopPattern = {
574
595
calc: function(price) {
575
596
price = discount(price);
@@ -631,8 +652,10 @@ export default ShopPattern;`
631
652
* Flyweight
632
653
*/
633
654
{
655
+ id : 'flyweight' ,
634
656
name : 'Flyweight' ,
635
657
type : 'structural' ,
658
+ hint : 'reduces the cost of creating and manipulating a large number of similar objects' ,
636
659
codeES5 : `function Color(name) {
637
660
this.name = name;
638
661
}
@@ -673,8 +696,11 @@ export { colorCreator };`
673
696
* Proxy
674
697
*/
675
698
{
699
+ id : 'proxy' ,
676
700
name : 'Proxy' ,
677
701
type : 'structural' ,
702
+ hint :
703
+ 'provides a placeholder for another object to control access, reduce cost, and reduce complexity' ,
678
704
codeES5 : `function Car() {
679
705
this.drive = function() {
680
706
return 'driving';
@@ -721,8 +747,10 @@ export { Car, CarPattern, Driver };`
721
747
* Chain of Resp
722
748
*/
723
749
{
724
- name : 'Chain of Resp' ,
750
+ id : 'chain_of_responsibility' ,
751
+ name : 'Chain of Responsibility' ,
725
752
type : 'behavioral' ,
753
+ hint : 'delegates commands to a chain of processing objects' ,
726
754
codeES5 : `function ShoppingCart() {
727
755
this.products = [];
728
756
@@ -851,8 +879,10 @@ export { ShoppingCart, Discount };`
851
879
* Command
852
880
*/
853
881
{
882
+ id : 'command' ,
854
883
name : 'Command' ,
855
884
type : 'behavioral' ,
885
+ hint : 'creates objects which encapsulate actions and parameters' ,
856
886
codeES5 : `function Cockpit(instruction) {
857
887
this.instruction = instruction;
858
888
}
@@ -961,8 +991,10 @@ export { Cockpit, Turbine, OnInstruction, OffInstruction };`
961
991
* Interpreter
962
992
*/
963
993
{
994
+ id : 'interpteter' ,
964
995
name : 'Interpreter' ,
965
996
type : 'behavioral' ,
997
+ hint : 'implements a specialized language' ,
966
998
codeES5 : `function Sum(left, right) {
967
999
this.left = left;
968
1000
this.right = right;
@@ -1028,8 +1060,11 @@ export { Num, Min, Sum };`
1028
1060
* Iterator
1029
1061
*/
1030
1062
{
1063
+ id : 'iterator' ,
1031
1064
name : 'Iterator' ,
1032
1065
type : 'behavioral' ,
1066
+ hint :
1067
+ 'accesses the elements of an object sequentially without exposing its underlying representation' ,
1033
1068
codeES5 : `function Pattern(el) {
1034
1069
this.index = 0;
1035
1070
this.elements = el;
@@ -1066,8 +1101,11 @@ export default Pattern;`
1066
1101
* Mediator
1067
1102
*/
1068
1103
{
1104
+ id : 'mediator' ,
1069
1105
name : 'Mediator' ,
1070
1106
type : 'behavioral' ,
1107
+ hint :
1108
+ 'allows loose coupling between classes by being the only class that has detailed knowledge of their methods' ,
1071
1109
codeES5 : `function TrafficTower() {
1072
1110
this.airplanes = [];
1073
1111
}
@@ -1119,8 +1157,10 @@ export { TrafficTower, Airplane };`
1119
1157
* Memento
1120
1158
*/
1121
1159
{
1160
+ id : 'memento' ,
1122
1161
name : 'Memento' ,
1123
1162
type : 'behavioral' ,
1163
+ hint : 'provides the ability to restore an object to its previous state' ,
1124
1164
codeES5 : `function Pattern(value) {
1125
1165
this.value = value;
1126
1166
}
@@ -1182,8 +1222,11 @@ export { originator, Caretaker };`
1182
1222
* Observer
1183
1223
*/
1184
1224
{
1225
+ id : 'observer' ,
1185
1226
name : 'Observer' ,
1186
1227
type : 'behavioral' ,
1228
+ hint :
1229
+ 'is a publish/subscribe pattern which allows a number of observer objects to see an event' ,
1187
1230
codeES5 : `function Product() {
1188
1231
this.price = 0;
1189
1232
this.actions = [];
@@ -1273,8 +1316,10 @@ export { Product, fees, proft };`
1273
1316
* State
1274
1317
*/
1275
1318
{
1319
+ id : 'state' ,
1276
1320
name : 'State' ,
1277
1321
type : 'behavioral' ,
1322
+ hint : 'allows an object to alter its behavior when its internal state changes' ,
1278
1323
codeES5 : `function Order() {
1279
1324
this.pattern = new WaitingForPayment();
1280
1325
@@ -1350,8 +1395,10 @@ export default Order;`
1350
1395
* Strategy
1351
1396
*/
1352
1397
{
1398
+ id : 'strategy' ,
1353
1399
name : 'Strategy' ,
1354
1400
type : 'behavioral' ,
1401
+ hint : 'allows one of a family of algorithms to be selected on-the-fly at runtime' ,
1355
1402
codeES5 : `function ShoppingCart(discount) {
1356
1403
this.discount = discount;
1357
1404
this.amount = 0;
@@ -1411,8 +1458,11 @@ export { ShoppingCart, guestPattern, regularPattern, premiumPattern };`
1411
1458
* Template
1412
1459
*/
1413
1460
{
1461
+ id : 'template' ,
1414
1462
name : 'Template' ,
1415
1463
type : 'behavioral' ,
1464
+ hint :
1465
+ 'defines the skeleton of an algorithm as an abstract class, allowing its subclasses to provide concrete behavior' ,
1416
1466
codeES5 : `function Tax() {}
1417
1467
1418
1468
Tax.prototype.calc = function(value) {
@@ -1476,8 +1526,11 @@ export { Tax1, Tax2 };`
1476
1526
* Visitor
1477
1527
*/
1478
1528
{
1529
+ id : 'visitor' ,
1479
1530
name : 'Visitor' ,
1480
1531
type : 'behavioral' ,
1532
+ hint :
1533
+ 'separates an algorithm from an object structure by moving the hierarchy of methods into one object' ,
1481
1534
codeES5 : `function bonusPattern(employee) {
1482
1535
if (employee instanceof Manager) employee.bonus = employee.salary * 2;
1483
1536
if (employee instanceof Developer) employee.bonus = employee.salary;
0 commit comments