From 742f4e1c67975ce63d8ea9fb591ca4adaf753515 Mon Sep 17 00:00:00 2001 From: Zoltan Toth Date: Fri, 1 Mar 2019 08:45:50 -0500 Subject: [PATCH 1/3] Removed pattern names from code samples --- src/data/patterns.js | 238 +++++++++++++++++++++---------------------- 1 file changed, 118 insertions(+), 120 deletions(-) diff --git a/src/data/patterns.js b/src/data/patterns.js index d851f85..b20e0c4 100644 --- a/src/data/patterns.js +++ b/src/data/patterns.js @@ -6,15 +6,15 @@ const patterns = [ name: 'Abstract Factory', type: 'creational', codeES5: `function droidProducer(kind) { - if (kind === 'battle') return battleDroidFactory; - return pilotDroidFactory; + if (kind === 'battle') return battleDroidPattern; + return pilotDroidPattern; } -function battleDroidFactory() { +function battleDroidPattern() { return new B1(); } -function pilotDroidFactory() { +function pilotDroidPattern() { return new Rx24(); } @@ -30,15 +30,15 @@ Rx24.prototype.info = function() { module.exports = droidProducer;`, codeES6: `function droidProducer(kind) { - if (kind === 'battle') return battleDroidFactory; - return pilotDroidFactory; + if (kind === 'battle') return battleDroidPattern; + return pilotDroidPattern; } -function battleDroidFactory() { +function battleDroidPattern() { return new B1(); } -function pilotDroidFactory() { +function pilotDroidPattern() { return new Rx24(); } @@ -68,7 +68,7 @@ export default droidProducer;` this.payload = {}; } -function RequestBuilder() { +function RequestPattern() { this.request = new Request(); this.forUrl = function(url) { @@ -91,7 +91,7 @@ function RequestBuilder() { }; } -module.exports = RequestBuilder;`, +module.exports = RequestPattern;`, codeES6: `class Request { constructor() { this.url = ''; @@ -100,7 +100,7 @@ module.exports = RequestBuilder;`, } } -class RequestBuilder { +class RequestPattern { constructor() { this.request = new Request(); } @@ -125,7 +125,7 @@ class RequestBuilder { } } -export default RequestBuilder;` +export default RequestPattern;` }, /* * Factory @@ -133,7 +133,7 @@ export default RequestBuilder;` { name: 'Factory', type: 'creational', - codeES5: `function bmwFactory(type) { + codeES5: `function bmwPattern(type) { if (type === 'X5') return new Bmw(type, 108000, 300); if (type === 'X6') return new Bmw(type, 111000, 320); } @@ -144,8 +144,8 @@ function Bmw(model, price, maxSpeed) { this.maxSpeed = maxSpeed; } -module.exports = bmwFactory;`, - codeES6: `class BmwFactory { +module.exports = bmwPattern;`, + codeES6: `class BmwPattern { create(type) { if (type === 'X5') return new Bmw(type, 108000, 300); if (type === 'X6') return new Bmw(type, 111000, 320); @@ -160,7 +160,7 @@ class Bmw { } } -export default BmwFactory;` +export default BmwPattern;` }, /* * Prototype @@ -240,15 +240,15 @@ Jedi.prototype.attackWithSaber = function() { return this.lvl * 100; }; -function JediAdapter(jedi) { +function JediPattern(jedi) { this.jedi = jedi; } -JediAdapter.prototype.attack = function() { +JediPattern.prototype.attack = function() { return this.jedi.attackWithSaber(); }; -module.exports = [Soldier, Jedi, JediAdapter];`, +module.exports = [Soldier, Jedi, JediPattern];`, codeES6: `class Soldier { constructor(level) { this.level = level; @@ -269,7 +269,7 @@ class Jedi { } } -class JediAdapter { +class JediPattern { constructor(jedi) { this.jedi = jedi; } @@ -279,7 +279,7 @@ class JediAdapter { } } -export { Soldier, Jedi, JediAdapter };` +export { Soldier, Jedi, JediPattern };` }, /* * Bridge @@ -365,17 +365,16 @@ export { EpsonPrinter, HPprinter, AcrylicInk, AlcoholInk };` { name: 'Composite', type: 'structural', - codeES5: `// composition -function EquipmentComposition(name) { + codeES5: `function EquipmentPattern(name) { this.equipments = []; this.name = name; } -EquipmentComposition.prototype.add = function(equipment) { +EquipmentPattern.prototype.add = function(equipment) { this.equipments.push(equipment); }; -EquipmentComposition.prototype.getPrice = function() { +EquipmentPattern.prototype.getPrice = function() { return this.equipments .map(function(equipment) { return equipment.getPrice(); @@ -410,7 +409,7 @@ function Memory() { } Memory.prototype = Object.create(Equipment.prototype); -module.exports = [EquipmentComposition, FloppyDisk, HardDrive, Memory];`, +module.exports = [EquipmentPattern, FloppyDisk, HardDrive, Memory];`, codeES6: `//Equipment class Equipment { getPrice() { @@ -426,8 +425,7 @@ class Equipment { } } -// --- composite --- -class Composite extends Equipment { +class Pattern extends Equipment { constructor() { super(); this.equipments = []; @@ -448,7 +446,7 @@ class Composite extends Equipment { } } -class Cabbinet extends Composite { +class Cabbinet extends Pattern { constructor() { super(); this.setName('cabbinet'); @@ -500,23 +498,23 @@ function Penne() { } Penne.prototype = Object.create(Pasta.prototype); -function SauceDecorator(pasta) { +function SaucePattern(pasta) { this.pasta = pasta; } -SauceDecorator.prototype.getPrice = function() { +SaucePattern.prototype.getPrice = function() { return this.pasta.getPrice() + 5; }; -function CheeseDecorator(pasta) { +function CheesePattern(pasta) { this.pasta = pasta; } -CheeseDecorator.prototype.getPrice = function() { +CheesePattern.prototype.getPrice = function() { return this.pasta.getPrice() + 3; }; -module.exports = [Penne, SauceDecorator, CheeseDecorator];`, +module.exports = [Penne, SaucePattern, CheesePattern];`, codeES6: `class Pasta { constructor() { this.price = 0; @@ -533,7 +531,7 @@ class Penne extends Pasta { } } -class PastaDecorator extends Pasta { +class PastaPattern extends Pasta { constructor(pasta) { super(); this.pasta = pasta; @@ -544,7 +542,7 @@ class PastaDecorator extends Pasta { } } -class SauceDecorator extends PastaDecorator { +class SaucePattern extends PastaPattern { constructor(pasta) { super(pasta); } @@ -554,7 +552,7 @@ class SauceDecorator extends PastaDecorator { } } -class CheeseDecorator extends PastaDecorator { +class CheesePattern extends PastaPattern { constructor(pasta) { super(pasta); } @@ -564,7 +562,7 @@ class CheeseDecorator extends PastaDecorator { } } -export { Penne, SauceDecorator, CheeseDecorator };` +export { Penne, SaucePattern, CheesePattern };` }, /* * Facade @@ -572,7 +570,7 @@ export { Penne, SauceDecorator, CheeseDecorator };` { name: 'Facade', type: 'structural', - codeES5: `var shopFacade = { + codeES5: `var shopPattern = { calc: function(price) { price = discount(price); price = fees(price); @@ -593,8 +591,8 @@ function fees(value) { return value * 1.05; } -module.exports = shopFacade;`, - codeES6: `class ShopFacade { +module.exports = shopPattern;`, + codeES6: `class ShopPattern { constructor() { this.discount = new Discount(); this.shipping = new Shipping(); @@ -627,7 +625,7 @@ class Fees { } } -export default ShopFacade;` +export default ShopPattern;` }, /* * Flyweight @@ -639,7 +637,7 @@ export default ShopFacade;` this.name = name; } -var colorFactory = { +var colorCreator = { colors: {}, create: function(name) { var color = this.colors[name]; @@ -650,14 +648,14 @@ var colorFactory = { } }; -module.exports = colorFactory;`, +module.exports = colorCreator;`, codeES6: `class Color { constructor(name) { this.name = name; } } -class colorFactory { +class colorCreator { constructor(name) { this.colors = {}; } @@ -669,7 +667,7 @@ class colorFactory { } } -export { colorFactory };` +export { colorCreator };` }, /* * Proxy @@ -683,7 +681,7 @@ export { colorFactory };` }; } -function CarProxy(driver) { +function CarPattern(driver) { this.driver = driver; this.drive = function() { if (driver.age < 18) return 'too young to drive'; @@ -695,14 +693,14 @@ function Driver(age) { this.age = age; } -module.exports = [Car, CarProxy, Driver];`, +module.exports = [Car, CarPattern, Driver];`, codeES6: `class Car { drive() { return 'driving'; } } -class CarProxy { +class CarPattern { constructor(driver) { this.driver = driver; } @@ -717,7 +715,7 @@ class Driver { } } -export { Car, CarProxy, Driver };` +export { Car, CarPattern, Driver };` }, /* * Chain of Resp @@ -855,11 +853,11 @@ export { ShoppingCart, Discount };` { name: 'Command', type: 'behavioral', - codeES5: `function Cockpit(command) { - this.command = command; + codeES5: `function Cockpit(instruction) { + this.instruction = instruction; } Cockpit.prototype.execute = function() { - this.command.execute(); + this.instruction.execute(); }; function Turbine() { @@ -889,41 +887,41 @@ Turbine.prototype.speedUp = function() { this.speed += 100; }; -function OnCommand(turbine) { +function OnInstruction(turbine) { this.turbine = turbine; } -OnCommand.prototype.execute = function() { +OnInstruction.prototype.execute = function() { this.turbine.on(); }; -function OffCommand(turbine) { +function OffInstruction(turbine) { this.turbine = turbine; } -OffCommand.prototype.execute = function() { +OffInstruction.prototype.execute = function() { this.turbine.off(); }; -function SpeedUpCommand(turbine) { +function SpeedUpInstruction(turbine) { this.turbine = turbine; } -SpeedUpCommand.prototype.execute = function() { +SpeedUpInstruction.prototype.execute = function() { this.turbine.speedUp(); }; -function SpeedDownCommand(turbine) { +function SpeedDownInstruction(turbine) { this.turbine = turbine; } -SpeedDownCommand.prototype.execute = function() { +SpeedDownInstruction.prototype.execute = function() { this.turbine.speedDown(); }; -module.exports = [Cockpit, Turbine, OnCommand, OffCommand, SpeedUpCommand, SpeedDownCommand];`, +module.exports = [Cockpit, Turbine, OnInstruction, OffInstruction, SpeedUpInstruction, SpeedDownInstruction];`, codeES6: `class Cockpit { - constructor(command) { - this.command = command; + constructor(instruction) { + this.instruction = instruction; } execute() { - this.command.execute(); + this.instruction.execute(); } } @@ -939,7 +937,7 @@ class Turbine { } } -class OnCommand { +class OnInstruction { constructor(turbine) { this.turbine = turbine; } @@ -948,7 +946,7 @@ class OnCommand { } } -class OffCommand { +class OffInstruction { constructor(turbine) { this.turbine = turbine; } @@ -957,7 +955,7 @@ class OffCommand { } } -export { Cockpit, Turbine, OnCommand, OffCommand };` +export { Cockpit, Turbine, OnInstruction, OffInstruction };` }, /* * Interpreter @@ -970,8 +968,8 @@ export { Cockpit, Turbine, OnCommand, OffCommand };` this.right = right; } -Sum.prototype.interpret = function() { - return this.left.interpret() + this.right.interpret(); +Sum.prototype.pattern = function() { + return this.left.pattern() + this.right.pattern(); }; function Min(left, right) { @@ -979,15 +977,15 @@ function Min(left, right) { this.right = right; } -Min.prototype.interpret = function() { - return this.left.interpret() - this.right.interpret(); +Min.prototype.pattern = function() { + return this.left.pattern() - this.right.pattern(); }; function Num(val) { this.val = val; } -Num.prototype.interpret = function() { +Num.prototype.pattern = function() { return this.val; }; @@ -998,8 +996,8 @@ module.exports = [Num, Min, Sum];`, this.right = right; } - interpret() { - return this.left.interpret() + this.right.interpret(); + pattern() { + return this.left.pattern() + this.right.pattern(); } } @@ -1009,8 +1007,8 @@ class Min { this.right = right; } - interpret() { - return this.left.interpret() - this.right.interpret(); + pattern() { + return this.left.pattern() - this.right.pattern(); } } @@ -1019,7 +1017,7 @@ class Num { this.val = val; } - interpret() { + pattern() { return this.val; } } @@ -1032,12 +1030,12 @@ export { Num, Min, Sum };` { name: 'Iterator', type: 'behavioral', - codeES5: `function Iterator(el) { + codeES5: `function Pattern(el) { this.index = 0; this.elements = el; } -Iterator.prototype = { +Pattern.prototype = { next: function() { return this.elements[this.index++]; }, @@ -1046,8 +1044,8 @@ Iterator.prototype = { } }; -module.exports = Iterator;`, - codeES6: `class Iterator { +module.exports = Pattern;`, + codeES6: `class Pattern { constructor(el) { this.index = 0; this.elements = el; @@ -1062,7 +1060,7 @@ module.exports = Iterator;`, } } -export default Iterator;` +export default Pattern;` }, /* * Mediator @@ -1123,16 +1121,16 @@ export { TrafficTower, Airplane };` { name: 'Memento', type: 'behavioral', - codeES5: `function Memento(value) { + codeES5: `function Pattern(value) { this.value = value; } var originator = { store: function(val) { - return new Memento(val); + return new Pattern(val); }, - restore: function(memento) { - return memento.value; + restore: function(pattern) { + return pattern.value; } }; @@ -1140,16 +1138,16 @@ function Caretaker() { this.values = []; } -Caretaker.prototype.addMemento = function(memento) { - this.values.push(memento); +Caretaker.prototype.addPattern = function(pattern) { + this.values.push(pattern); }; -Caretaker.prototype.getMemento = function(index) { +Caretaker.prototype.getPattern = function(index) { return this.values[index]; }; module.exports = [originator, Caretaker];`, - codeES6: `class Memento { + codeES6: `class Pattern { constructor(value) { this.value = value; } @@ -1157,10 +1155,10 @@ module.exports = [originator, Caretaker];`, const originator = { store: function(val) { - return new Memento(val); + return new Pattern(val); }, - restore: function(memento) { - return memento.value; + restore: function(pattern) { + return pattern.value; } }; @@ -1169,11 +1167,11 @@ class Caretaker { this.values = []; } - addMemento(memento) { - this.values.push(memento); + addPattern(pattern) { + this.values.push(pattern); } - getMemento(index) { + getPattern(index) { return this.values[index]; } } @@ -1275,13 +1273,13 @@ export { Product, fees, proft };` * State */ { - name: 'State', + name: 'Pattern', type: 'behavioral', codeES5: `function Order() { - this.state = new WaitingForPayment(); + this.pattern = new WaitingForPayment(); - this.nextState = function() { - this.state = this.state.next(); + this.nextPattern = function() { + this.pattern = this.pattern.next(); }; } @@ -1338,11 +1336,11 @@ class Delivered extends OrderStatus { class Order { constructor() { - this.state = new WaitingForPayment(); + this.pattern = new WaitingForPayment(); } - nextState() { - this.state = this.state.next(); + nextPattern() { + this.pattern = this.pattern.next(); } } @@ -1367,19 +1365,19 @@ ShoppingCart.prototype.checkout = function() { return this.discount(this.amount); }; -function guestStrategy(amount) { +function guestPattern(amount) { return amount; } -function regularStrategy(amount) { +function regularPattern(amount) { return amount * 0.9; } -function premiumStrategy(amount) { +function premiumPattern(amount) { return amount * 0.8; } -module.exports = [ShoppingCart, guestStrategy, regularStrategy, premiumStrategy];`, +module.exports = [ShoppingCart, guestPattern, regularPattern, premiumPattern];`, codeES6: `class ShoppingCart { constructor(discount) { this.discount = discount; @@ -1395,19 +1393,19 @@ module.exports = [ShoppingCart, guestStrategy, regularStrategy, premiumStrategy] } } -function guestStrategy(amount) { +function guestPattern(amount) { return amount; } -function regularStrategy(amount) { +function regularPattern(amount) { return amount * 0.9; } -function premiumStrategy(amount) { +function premiumPattern(amount) { return amount * 0.8; } -export { ShoppingCart, guestStrategy, regularStrategy, premiumStrategy };` +export { ShoppingCart, guestPattern, regularPattern, premiumPattern };` }, /* * Template @@ -1480,7 +1478,7 @@ export { Tax1, Tax2 };` { name: 'Visitor', type: 'behavioral', - codeES5: `function bonusVisitor(employee) { + codeES5: `function bonusPattern(employee) { if (employee instanceof Manager) employee.bonus = employee.salary * 2; if (employee instanceof Developer) employee.bonus = employee.salary; } @@ -1489,8 +1487,8 @@ function Employee() { this.bonus = 0; } -Employee.prototype.accept = function(visitor) { - visitor(this); +Employee.prototype.accept = function(item) { + item(this); }; function Manager(salary) { @@ -1505,8 +1503,8 @@ function Developer(salary) { Developer.prototype = Object.create(Employee.prototype); -module.exports = [Developer, Manager, bonusVisitor];`, - codeES6: `function bonusVisitor(employee) { +module.exports = [Developer, Manager, bonusPattern];`, + codeES6: `function bonusPattern(employee) { if (employee instanceof Manager) employee.bonus = employee.salary * 2; if (employee instanceof Developer) employee.bonus = employee.salary; } @@ -1517,8 +1515,8 @@ class Employee { this.salary = salary; } - accept(visitor) { - visitor(this); + accept(item) { + item(this); } } @@ -1534,7 +1532,7 @@ class Developer extends Employee { } } -export { Developer, Manager, bonusVisitor };` +export { Developer, Manager, bonusPattern };` } ]; From aae421bf0fbaa1e2019629aa44bfb5f448cd5710 Mon Sep 17 00:00:00 2001 From: Zoltan Toth Date: Fri, 1 Mar 2019 09:04:54 -0500 Subject: [PATCH 2/3] Incorrect pattern name fixed --- src/data/patterns.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/data/patterns.js b/src/data/patterns.js index b20e0c4..27ec475 100644 --- a/src/data/patterns.js +++ b/src/data/patterns.js @@ -133,26 +133,26 @@ export default RequestPattern;` { name: 'Factory', type: 'creational', - codeES5: `function bmwPattern(type) { - if (type === 'X5') return new Bmw(type, 108000, 300); - if (type === 'X6') return new Bmw(type, 111000, 320); + codeES5: `function teslaPattern(type) { + if (type === 'ModelX') return new Tesla(type, 108000, 300); + if (type === 'ModelS') return new Tesla(type, 111000, 320); } -function Bmw(model, price, maxSpeed) { +function Tesla(model, price, maxSpeed) { this.model = model; this.price = price; this.maxSpeed = maxSpeed; } -module.exports = bmwPattern;`, - codeES6: `class BmwPattern { +module.exports = teslaPattern;`, + codeES6: `class TeslaPattern { create(type) { - if (type === 'X5') return new Bmw(type, 108000, 300); - if (type === 'X6') return new Bmw(type, 111000, 320); + if (type === 'ModelX') return new Tesla(type, 108000, 300); + if (type === 'ModelS') return new Tesla(type, 111000, 320); } } -class Bmw { +class Tesla { constructor(model, price, maxSpeed) { this.model = model; this.price = price; @@ -160,7 +160,7 @@ class Bmw { } } -export default BmwPattern;` +export default TeslaPattern;` }, /* * Prototype @@ -1273,7 +1273,7 @@ export { Product, fees, proft };` * State */ { - name: 'Pattern', + name: 'State', type: 'behavioral', codeES5: `function Order() { this.pattern = new WaitingForPayment(); From 727555f526715b4fe18a4c42835d6cf8a653dd51 Mon Sep 17 00:00:00 2001 From: Zoltan Toth Date: Fri, 1 Mar 2019 09:47:27 -0500 Subject: [PATCH 3/3] Add hint and id to patterns in data file --- src/data/patterns.js | 55 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/src/data/patterns.js b/src/data/patterns.js index 27ec475..3895c07 100644 --- a/src/data/patterns.js +++ b/src/data/patterns.js @@ -3,8 +3,10 @@ const patterns = [ * Abstract */ { + id: 'abstract_factory', name: 'Abstract Factory', type: 'creational', + hint: 'groups object factories that have a common theme', codeES5: `function droidProducer(kind) { if (kind === 'battle') return battleDroidPattern; return pilotDroidPattern; @@ -60,8 +62,10 @@ export default droidProducer;` * Builder */ { + id: 'builder', name: 'Builder', type: 'creational', + hint: 'constructs complex objects by separating construction and representation', codeES5: `function Request() { this.url = ''; this.method = ''; @@ -131,8 +135,10 @@ export default RequestPattern;` * Factory */ { + id: 'factory', name: 'Factory', type: 'creational', + hint: 'creates objects without specifying the exact class to create', codeES5: `function teslaPattern(type) { if (type === 'ModelX') return new Tesla(type, 108000, 300); if (type === 'ModelS') return new Tesla(type, 111000, 320); @@ -166,8 +172,10 @@ export default TeslaPattern;` * Prototype */ { + id: 'prototype', name: 'Prototype', type: 'creational', + hint: 'creates objects by cloning an existing object', codeES5: `function Sheep(name, weight) { this.name = name; this.weight = weight; @@ -195,8 +203,10 @@ export default Sheep;` * Singleton */ { + id: 'singleton', name: 'Singleton', type: 'creational', + hint: 'restricts object creation for a class to only one instance', codeES5: `function Person() { if (typeof Person.instance === 'object') return Person.instance; @@ -222,8 +232,11 @@ export default Person;` * Adapter */ { + id: 'adapter', name: 'Adapter', type: 'structural', + hint: + 'allows classes with incompatible interfaces to work together by wrapping its own interface around that of an already existing class', codeES5: `function Soldier(lvl) { this.lvl = lvl; } @@ -285,8 +298,10 @@ export { Soldier, Jedi, JediPattern };` * Bridge */ { + id: 'bridge', name: 'Bridge', type: 'structural', + hint: 'decouples an abstraction from its implementation so that the two can vary independently', codeES5: `function EpsonPrinter(ink) { this.ink = ink(); } @@ -363,8 +378,10 @@ export { EpsonPrinter, HPprinter, AcrylicInk, AlcoholInk };` * Composite */ { + id: 'composite', name: 'Composite', type: 'structural', + hint: 'composes zero-or-more similar objects so that they can be manipulated as one object', codeES5: `function EquipmentPattern(name) { this.equipments = []; this.name = name; @@ -484,8 +501,10 @@ export { Cabbinet, FloppyDisk, HardDrive, Memory };` * Decorator */ { + id: 'decorator', name: 'Decorator', type: 'structural', + hint: 'dynamically adds/overrides behaviour in an existing method of an object', codeES5: `function Pasta() { this.price = 0; } @@ -568,8 +587,10 @@ export { Penne, SaucePattern, CheesePattern };` * Facade */ { + id: 'facade', name: 'Facade', type: 'structural', + hint: 'provides a simplified interface to a large body of code', codeES5: `var shopPattern = { calc: function(price) { price = discount(price); @@ -631,8 +652,10 @@ export default ShopPattern;` * Flyweight */ { + id: 'flyweight', name: 'Flyweight', type: 'structural', + hint: 'reduces the cost of creating and manipulating a large number of similar objects', codeES5: `function Color(name) { this.name = name; } @@ -673,8 +696,11 @@ export { colorCreator };` * Proxy */ { + id: 'proxy', name: 'Proxy', type: 'structural', + hint: + 'provides a placeholder for another object to control access, reduce cost, and reduce complexity', codeES5: `function Car() { this.drive = function() { return 'driving'; @@ -721,8 +747,10 @@ export { Car, CarPattern, Driver };` * Chain of Resp */ { - name: 'Chain of Resp', + id: 'chain_of_responsibility', + name: 'Chain of Responsibility', type: 'behavioral', + hint: 'delegates commands to a chain of processing objects', codeES5: `function ShoppingCart() { this.products = []; @@ -851,8 +879,10 @@ export { ShoppingCart, Discount };` * Command */ { + id: 'command', name: 'Command', type: 'behavioral', + hint: 'creates objects which encapsulate actions and parameters', codeES5: `function Cockpit(instruction) { this.instruction = instruction; } @@ -961,8 +991,10 @@ export { Cockpit, Turbine, OnInstruction, OffInstruction };` * Interpreter */ { + id: 'interpteter', name: 'Interpreter', type: 'behavioral', + hint: 'implements a specialized language', codeES5: `function Sum(left, right) { this.left = left; this.right = right; @@ -1028,8 +1060,11 @@ export { Num, Min, Sum };` * Iterator */ { + id: 'iterator', name: 'Iterator', type: 'behavioral', + hint: + 'accesses the elements of an object sequentially without exposing its underlying representation', codeES5: `function Pattern(el) { this.index = 0; this.elements = el; @@ -1066,8 +1101,11 @@ export default Pattern;` * Mediator */ { + id: 'mediator', name: 'Mediator', type: 'behavioral', + hint: + 'allows loose coupling between classes by being the only class that has detailed knowledge of their methods', codeES5: `function TrafficTower() { this.airplanes = []; } @@ -1119,8 +1157,10 @@ export { TrafficTower, Airplane };` * Memento */ { + id: 'memento', name: 'Memento', type: 'behavioral', + hint: 'provides the ability to restore an object to its previous state', codeES5: `function Pattern(value) { this.value = value; } @@ -1182,8 +1222,11 @@ export { originator, Caretaker };` * Observer */ { + id: 'observer', name: 'Observer', type: 'behavioral', + hint: + 'is a publish/subscribe pattern which allows a number of observer objects to see an event', codeES5: `function Product() { this.price = 0; this.actions = []; @@ -1273,8 +1316,10 @@ export { Product, fees, proft };` * State */ { + id: 'state', name: 'State', type: 'behavioral', + hint: 'allows an object to alter its behavior when its internal state changes', codeES5: `function Order() { this.pattern = new WaitingForPayment(); @@ -1350,8 +1395,10 @@ export default Order;` * Strategy */ { + id: 'strategy', name: 'Strategy', type: 'behavioral', + hint: 'allows one of a family of algorithms to be selected on-the-fly at runtime', codeES5: `function ShoppingCart(discount) { this.discount = discount; this.amount = 0; @@ -1411,8 +1458,11 @@ export { ShoppingCart, guestPattern, regularPattern, premiumPattern };` * Template */ { + id: 'template', name: 'Template', type: 'behavioral', + hint: + 'defines the skeleton of an algorithm as an abstract class, allowing its subclasses to provide concrete behavior', codeES5: `function Tax() {} Tax.prototype.calc = function(value) { @@ -1476,8 +1526,11 @@ export { Tax1, Tax2 };` * Visitor */ { + id: 'visitor', name: 'Visitor', type: 'behavioral', + hint: + 'separates an algorithm from an object structure by moving the hierarchy of methods into one object', codeES5: `function bonusPattern(employee) { if (employee instanceof Manager) employee.bonus = employee.salary * 2; if (employee instanceof Developer) employee.bonus = employee.salary;