This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
57 changed files
with
737 additions
and
5 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
Node.js-design-patterns/Behaviorals/ChainOfResponsibilities/Storage.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class Storage { | ||
|
||
constructor(name, inventory=[], deliveryTime=0) { | ||
this.name = name; | ||
this.inventory = inventory; | ||
this.deliveryTime = deliveryTime; | ||
this.next = null; | ||
} | ||
|
||
lookInLocalInventory(itemName) { | ||
let index = this.inventory.map(item => item.name).indexOf(itemName); | ||
return this.inventory[index]; | ||
} | ||
|
||
setNext(storage) { | ||
this.next = storage; | ||
} | ||
|
||
find(itemName) { | ||
let found = this.lookInLocalInventory(itemName); | ||
if (found) { | ||
return { | ||
name: found.name, | ||
qty: found.qty, | ||
location: this.name, | ||
deliveryTime: (this.deliveryTime === 0) ? 'now' : `${this.deliveryTime} day(s)` | ||
} | ||
} else if (this.next) { | ||
return this.next.find(itemName); | ||
} else { | ||
return `we do not carry ${itemName}`; | ||
} | ||
} | ||
|
||
} | ||
|
||
module.exports = Storage; |
27 changes: 27 additions & 0 deletions
27
Node.js-design-patterns/Behaviorals/ChainOfResponsibilities/Store.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
let Storage = require('./Storage'); | ||
|
||
class Store { | ||
|
||
constructor(name, inventory=[]) { | ||
this.name = name; | ||
|
||
let floor = new Storage('store floor', inventory.floor); | ||
let backroom = new Storage('store backroom', inventory.backroom); | ||
let localStore = new Storage('nearby store', inventory.localStore, 1); | ||
let warehouse = new Storage('warehouse', inventory.warehouse, 5); | ||
|
||
floor.setNext(backroom); | ||
backroom.setNext(localStore); | ||
localStore.setNext(warehouse); | ||
|
||
this.storage = floor; | ||
|
||
} | ||
|
||
find(itemName) { | ||
return this.storage.find(itemName); | ||
} | ||
|
||
} | ||
|
||
module.exports = Store; |
9 changes: 9 additions & 0 deletions
9
Node.js-design-patterns/Behaviorals/ChainOfResponsibilities/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
let Store = require('./Store'); | ||
let inventory = require('./inventory'); | ||
|
||
let skiShop = new Store('Steep and Deep', inventory); | ||
|
||
let searchItem = 'powder skis'; | ||
let results = skiShop.find(searchItem); | ||
|
||
console.log( results ); |
29 changes: 29 additions & 0 deletions
29
Node.js-design-patterns/Behaviorals/ChainOfResponsibilities/inventory.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"floor": [ | ||
{ "name": "ski googles", "qty": 5 }, | ||
{ "name": "ski hats", "qty": 15 }, | ||
{ "name": "all mountain skis", "qty": 2 }, | ||
{ "name": "ski boots", "qty": 2 } | ||
], | ||
"backroom": [ | ||
{ "name": "ski googles", "qty": 5 }, | ||
{ "name": "ski hats", "qty": 15 }, | ||
{ "name": "ski poles", "qty": 2 }, | ||
{ "name": "ski rack", "qty": 1 } | ||
], | ||
"localStore": [ | ||
{ "name": "ski boots", "qty": 2 }, | ||
{ "name": "ski poles", "qty": 4 }, | ||
{ "name": "wax", "qty": 8 } | ||
], | ||
"warehouse": [ | ||
{ "name": "ski googles", "qty": 100 }, | ||
{ "name": "ski hats", "qty": 100 }, | ||
{ "name": "all mountain skis", "qty": 10 }, | ||
{ "name": "ski boots", "qty": 20 }, | ||
{ "name": "ski poles", "qty": 20 }, | ||
{ "name": "wax", "qty": 100 }, | ||
{ "name": "powder skis", "qty": 10 }, | ||
{ "name": "ski rack", "qty": 3 } | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
## Chain of responsibility | ||
|
||
Intent : "Avoid coupling the sender of a request to its receiver by giving than one object a change to handle the request. Chain the receiving objects and pass the request a long the chain" | ||
|
||
## Command | ||
|
||
Intent : "Encapsulate a request as an object, thereby letting you parameterize with different requests, queue or log requests, and support undoable operations." |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions
17
Node.js-design-patterns/Creationals/Factory Method/Problem/Employee.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var Shopper = require('./Shopper') | ||
|
||
class Employee extends Shopper { | ||
|
||
constructor(name, money=0, employer='') { | ||
super(name, money); | ||
this.employer = employer; | ||
this.employed = true; | ||
} | ||
|
||
payDay(money=0) { | ||
this.money += money; | ||
} | ||
|
||
} | ||
|
||
module.exports = Employee; |
13 changes: 13 additions & 0 deletions
13
Node.js-design-patterns/Creationals/Factory Method/Problem/Person.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Person { | ||
|
||
constructor(name='unnamed person') { | ||
this.name = name; | ||
} | ||
|
||
toString() { | ||
return JSON.stringify(this); | ||
} | ||
|
||
} | ||
|
||
module.exports = Person; |
13 changes: 13 additions & 0 deletions
13
Node.js-design-patterns/Creationals/Factory Method/Problem/Shopper.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var Person = require('./Person') | ||
|
||
class Shopper extends Person { | ||
|
||
constructor(name, money=0) { | ||
super(name); | ||
this.money = money; | ||
this.employed = false; | ||
} | ||
|
||
} | ||
|
||
module.exports = Shopper; |
10 changes: 10 additions & 0 deletions
10
Node.js-design-patterns/Creationals/Factory Method/Problem/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
var Shopper = require('./Shopper'); | ||
var Employee = require('./Employee'); | ||
|
||
// El problema recide cuando se se manajan muchos tipos de personas o cuando se requiere | ||
// En tiempo de ejecucion obeter un determinado tipo de persona | ||
var alex = new Shopper('Alex Banks', 100); | ||
var eve = new Employee('Eve Porcello', 100, 'This and That'); | ||
|
||
console.log( alex.toString() ) | ||
console.log( eve.toString() ) |
12 changes: 12 additions & 0 deletions
12
Node.js-design-patterns/Creationals/Factory Method/Solution/Developer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
var Person = require('./Person') | ||
|
||
class Developer extends Person{ | ||
|
||
constructor(name, money=0,skills) { | ||
super(name); | ||
this.money = money; | ||
this.skills = [...skills]; | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
Node.js-design-patterns/Creationals/Factory Method/Solution/Employee.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var Shopper = require('./Shopper') | ||
|
||
class Employee extends Shopper { | ||
|
||
constructor(name, money=0, employer='') { | ||
super(name, money); | ||
this.employer = employer; | ||
this.employed = true; | ||
} | ||
|
||
payDay(money=0) { | ||
this.money += money; | ||
} | ||
|
||
} | ||
|
||
module.exports = Employee; |
13 changes: 13 additions & 0 deletions
13
Node.js-design-patterns/Creationals/Factory Method/Solution/Person.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Person { | ||
|
||
constructor(name='unnamed person') { | ||
this.name = name; | ||
} | ||
|
||
toString() { | ||
return JSON.stringify(this); | ||
} | ||
|
||
} | ||
|
||
module.exports = Person; |
13 changes: 13 additions & 0 deletions
13
Node.js-design-patterns/Creationals/Factory Method/Solution/Shopper.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var Person = require('./Person') | ||
|
||
class Shopper extends Person { | ||
|
||
constructor(name, money=0) { | ||
super(name); | ||
this.money = money; | ||
this.employed = false; | ||
} | ||
|
||
} | ||
|
||
module.exports = Shopper; |
9 changes: 9 additions & 0 deletions
9
Node.js-design-patterns/Creationals/Factory Method/Solution/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
var userFactory = require('./userFactory'); | ||
|
||
var alex = userFactory('Alex Banks', 100); | ||
var eve = userFactory('Eve Porcello', 100, 'employee', 'This and That'); | ||
|
||
eve.payDay(100); | ||
|
||
console.log( alex.toString() ); | ||
console.log( eve.toString() ); |
12 changes: 12 additions & 0 deletions
12
Node.js-design-patterns/Creationals/Factory Method/Solution/userFactory.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var Employee = require('./Employee'); | ||
var Shopper = require('./Shopper'); | ||
|
||
var userFactory = (name, money=0, type, employer) => { | ||
if (type === 'employee') { | ||
return new Employee(name, money, employer); | ||
} else { | ||
return new Shopper(name, money); | ||
} | ||
} | ||
|
||
module.exports = userFactory; |
26 changes: 26 additions & 0 deletions
26
Node.js-design-patterns/Creationals/Prototype/Problem/Shopper.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class Shopper { | ||
|
||
constructor(name='unnamed person') { | ||
this._name = name; | ||
this._shoppingList = []; | ||
} | ||
|
||
set name(value) { | ||
this._name = value; | ||
} | ||
|
||
get name() { | ||
return this._name; | ||
} | ||
|
||
get shoppingList() { | ||
return this._shoppingList.join(', '); | ||
} | ||
|
||
addItemToList(item) { | ||
this._shoppingList.push(item); | ||
} | ||
|
||
} | ||
|
||
module.exports = Shopper; |
21 changes: 21 additions & 0 deletions
21
Node.js-design-patterns/Creationals/Prototype/Problem/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
let Shopper = require('./Shopper'); | ||
|
||
let alex = new Shopper('Alex Banks'); | ||
|
||
// Estos atributos se repiten y las lineas tambien | ||
alex.addItemToList('camping knife'); | ||
alex.addItemToList('tent'); | ||
alex.addItemToList('backpack'); | ||
alex.addItemToList('map'); | ||
//---- | ||
alex.addItemToList('slingshot'); | ||
|
||
let eve = new Shopper('Eve Porcello'); | ||
eve.addItemToList('camping knife'); | ||
eve.addItemToList('tent'); | ||
eve.addItemToList('backpack'); | ||
eve.addItemToList('map'); | ||
eve.addItemToList('reading light'); | ||
|
||
console.log( `${alex.name}: ${alex.shoppingList}` ); | ||
console.log( `${eve.name}: ${eve.shoppingList}` ); |
33 changes: 33 additions & 0 deletions
33
Node.js-design-patterns/Creationals/Prototype/Solution/Shopper.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class Shopper { | ||
|
||
constructor(name='unnamed person') { | ||
this._name = name; | ||
this._shoppingList = []; | ||
} | ||
|
||
set name(value) { | ||
this._name = value; | ||
} | ||
|
||
get name() { | ||
return this._name; | ||
} | ||
|
||
get shoppingList() { | ||
return this._shoppingList.join(', '); | ||
} | ||
|
||
addItemToList(item) { | ||
this._shoppingList.push(item); | ||
} | ||
clone(){ | ||
let proto = Object.getPrototypeOf(this); | ||
let cloned = Object.create(proto); | ||
cloned._name = this._name; | ||
cloned._shoppingList = [...this._shoppingList]; | ||
return cloned; | ||
|
||
} | ||
} | ||
|
||
module.exports = Shopper; |
12 changes: 12 additions & 0 deletions
12
Node.js-design-patterns/Creationals/Prototype/Solution/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
let scout_prototype = require('./scout_prototype'); | ||
|
||
let alex = scout_prototype.clone(); | ||
alex.name = 'Alex Banks'; | ||
alex.addItemToList('slingshot'); | ||
|
||
let eve = scout_prototype.clone(); | ||
eve.name = 'Eve Porcello'; | ||
eve.addItemToList('reading light'); | ||
|
||
console.log( `${alex.name}: ${alex.shoppingList}` ); | ||
console.log( `${eve.name}: ${eve.shoppingList}` ); |
9 changes: 9 additions & 0 deletions
9
Node.js-design-patterns/Creationals/Prototype/Solution/scout_prototype.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
var Shopper = require('./Shopper'); | ||
|
||
var scout = new Shopper(); | ||
scout.addItemToList('camping knife'); | ||
scout.addItemToList('tent'); | ||
scout.addItemToList('backpack'); | ||
scout.addItemToList('map'); | ||
|
||
module.exports = scout; |
19 changes: 19 additions & 0 deletions
19
Node.js-design-patterns/Creationals/Singleton/problem/Logger.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Logger { | ||
|
||
constructor() { | ||
this.logs = []; | ||
} | ||
|
||
get count() { | ||
return this.logs.length; | ||
} | ||
|
||
log(message) { | ||
const timestamp = new Date().toISOString(); | ||
this.logs.push({ message, timestamp }); | ||
console.log(`${timestamp} - ${message}`); | ||
} | ||
|
||
} | ||
|
||
module.exports = Logger; |
15 changes: 15 additions & 0 deletions
15
Node.js-design-patterns/Creationals/Singleton/problem/Shopper.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
var Logger = require('./Logger'); | ||
|
||
var logger = new Logger(); | ||
|
||
class Shopper { | ||
|
||
constructor(name, money=0) { | ||
this.name = name; | ||
this.money = money; | ||
logger.log(`New Shopper: ${name} has ${money} in their account.`); | ||
} | ||
|
||
} | ||
|
||
module.exports = Shopper; |
Oops, something went wrong.