diff --git a/Bridge/index.html b/Bridge/index.html new file mode 100644 index 0000000..d1d1d0c --- /dev/null +++ b/Bridge/index.html @@ -0,0 +1,36 @@ + + + + Bridge Pattern + + +
+

Source

+
+import GreyColor from './GreyColor';
+import FamilyCar from './FamilyCar';
+import MatteBlackColor from './MatteBlackColor';
+import Adventure4x4Car from './Adventure4X4Car';
+import RedColor from './RedColor';
+import UrbanCar from './UrbanCar';
+
+const familyCar = new FamilyCar(new GreyColor());
+const adventureCar = new Adventure4x4Car(new MatteBlackColor());
+const urbanCar = new UrbanCar(new RedColor());
+
+familyCar.applyColor();
+adventureCar.applyColor();
+urbanCar.applyColor();
+      
+
+
+

Console

+ +

BRIDGE

+
+ + + + + + \ No newline at end of file diff --git a/Bridge/scripts/Adventure4X4Car.js b/Bridge/scripts/Adventure4X4Car.js new file mode 100644 index 0000000..22951be --- /dev/null +++ b/Bridge/scripts/Adventure4X4Car.js @@ -0,0 +1,9 @@ +import Car from './Car'; + +class Adventure4x4Car extends Car { + constructor(color) { + super('4x4 Adventure car', 'For people that does not care about existing paths', 55000, 2, color); + } +} + +export default Adventure4x4Car; \ No newline at end of file diff --git a/Bridge/scripts/Car.js b/Bridge/scripts/Car.js new file mode 100644 index 0000000..db25683 --- /dev/null +++ b/Bridge/scripts/Car.js @@ -0,0 +1,15 @@ +class Car { + constructor(name, description, price, places = 2, color, brand = 'Cartisfaction') { + this.brand = brand; + this.name = name; + this.description = description; + this.price = price; + this.places = places; + this.color = color; + } + applyColor() { + console.log(`${this.name} car painted with color ${this.color.applyColor()}`); + } +} + +export default Car; \ No newline at end of file diff --git a/Bridge/scripts/Color.js b/Bridge/scripts/Color.js new file mode 100644 index 0000000..4a85002 --- /dev/null +++ b/Bridge/scripts/Color.js @@ -0,0 +1,7 @@ +class Color { + applyColor() { + throw new Error('This method should be overwritten'); + } +} + +export default Color; \ No newline at end of file diff --git a/Bridge/scripts/FamilyCar.js b/Bridge/scripts/FamilyCar.js new file mode 100644 index 0000000..e8899d0 --- /dev/null +++ b/Bridge/scripts/FamilyCar.js @@ -0,0 +1,9 @@ +import Car from './Car'; + +class FamilyCar extends Car { + constructor(color) { + super('Family car', 'Enjoy with your family', 30000, 5, color); + } +} + +export default FamilyCar; \ No newline at end of file diff --git a/Bridge/scripts/GreyColor.js b/Bridge/scripts/GreyColor.js new file mode 100644 index 0000000..e805aaa --- /dev/null +++ b/Bridge/scripts/GreyColor.js @@ -0,0 +1,9 @@ +import Color from './Color'; + +class GreyColor extends Color { + applyColor() { + return 'grey'; + } +} + +export default GreyColor; \ No newline at end of file diff --git a/Bridge/scripts/MatteBlackColor.js b/Bridge/scripts/MatteBlackColor.js new file mode 100644 index 0000000..4d6e4ee --- /dev/null +++ b/Bridge/scripts/MatteBlackColor.js @@ -0,0 +1,9 @@ +import Color from './Color'; + +class MatteBlackColor extends Color { + applyColor() { + return 'matte black'; + } +} + +export default MatteBlackColor; \ No newline at end of file diff --git a/Bridge/scripts/RedColor.js b/Bridge/scripts/RedColor.js new file mode 100644 index 0000000..efc23a0 --- /dev/null +++ b/Bridge/scripts/RedColor.js @@ -0,0 +1,9 @@ +import Color from './Color'; + +class RedColor extends Color { + applyColor() { + return 'red'; + } +} + +export default RedColor; \ No newline at end of file diff --git a/Bridge/scripts/UrbanCar.js b/Bridge/scripts/UrbanCar.js new file mode 100644 index 0000000..cf16868 --- /dev/null +++ b/Bridge/scripts/UrbanCar.js @@ -0,0 +1,9 @@ +import Car from './Car'; + +class UrbanCar extends Car { + constructor(color) { + super('Urban car', 'Small and designed for the city', 12000, 2, color); + } +} + +export default UrbanCar; \ No newline at end of file diff --git a/Bridge/scripts/main.js b/Bridge/scripts/main.js new file mode 100644 index 0000000..2598f98 --- /dev/null +++ b/Bridge/scripts/main.js @@ -0,0 +1,14 @@ +import GreyColor from './GreyColor'; +import FamilyCar from './FamilyCar'; +import MatteBlackColor from './MatteBlackColor'; +import Adventure4x4Car from './Adventure4X4Car'; +import RedColor from './RedColor'; +import UrbanCar from './UrbanCar'; + +const familyCar = new FamilyCar(new GreyColor()); +const adventureCar = new Adventure4x4Car(new MatteBlackColor()); +const urbanCar = new UrbanCar(new RedColor()); + +familyCar.applyColor(); +adventureCar.applyColor(); +urbanCar.applyColor(); \ No newline at end of file diff --git a/Builder/index.html b/Builder/index.html new file mode 100644 index 0000000..bde45b6 --- /dev/null +++ b/Builder/index.html @@ -0,0 +1,35 @@ + + + + Builder Pattern + + +
+

Source

+
+import MealBuilder from './MealBuilder';
+
+var mealBuilder = new MealBuilder();
+
+var veganMeal = mealBuilder.prepareVeganMeal();
+veganMeal.showItems();
+console.log(`Vegan meal cost: $${veganMeal.getCost()}`);
+
+var nonVeganMeal = mealBuilder.prepareNonVeganMeal();
+nonVeganMeal.showItems();
+console.log(`Non vegan meal cost: $${nonVeganMeal.getCost()}`);
+
+var deluxeMeal = mealBuilder.prepareDeluxeMeal();
+deluxeMeal.showItems();
+console.log(`Deluxe meal cost: $${deluxeMeal.getCost()}`);        
+      
+
+
+

Console

+ +

Builder

+
+ + + + diff --git a/Builder/scripts/Item.js b/Builder/scripts/Item.js new file mode 100644 index 0000000..31f0a67 --- /dev/null +++ b/Builder/scripts/Item.js @@ -0,0 +1,13 @@ +class Item { + get name() { + throw new Error('This method should be implemented'); + } + get packing() { + throw new Error('This method should be implemented'); + } + get price() { + throw new Error('This method should be implemented'); + } +} + +export default Item; \ No newline at end of file diff --git a/Builder/scripts/Meal.js b/Builder/scripts/Meal.js new file mode 100644 index 0000000..9150c16 --- /dev/null +++ b/Builder/scripts/Meal.js @@ -0,0 +1,25 @@ +const itemList = new WeakMap(); +class Meal { + get list() { + if(!itemList.get(this)) { + itemList.set(this, []); + } + return itemList.get(this); + } + addItem(item) { + this.list.push(item); + } + getCost() { + return this.list.reduce((accum, item) => { + accum += item.price; + return accum; + }, 0); + } + showItems() { + this.list.forEach((item) => { + console.log(`Item: ${item.name}, Packing: ${item.packing.pack()}, Price: $${item.price}`); + }); + } +} + +export default Meal; \ No newline at end of file diff --git a/Builder/scripts/MealBuilder.js b/Builder/scripts/MealBuilder.js new file mode 100644 index 0000000..d04b3aa --- /dev/null +++ b/Builder/scripts/MealBuilder.js @@ -0,0 +1,36 @@ +import Meal from './Meal'; +import VeganBurger from './items/burgers/VeganBurger'; +import BeefBurger from './items/burgers/BeefBurger'; +import KobeBurger from './items/burgers/KobeBurger'; +import Water from './items/drinks/Water'; +import Coke from './items/drinks/Coke'; +import Champagne from './items/drinks/Champagne'; +import Salad from './items/side-dishes/Salad'; +import Fries from './items/side-dishes/Fries'; +import Crudettes from './items/side-dishes/Crudettes'; + +class MealBuilder { + prepareVeganMeal() { + var meal = new Meal(); + meal.addItem(new VeganBurger()); + meal.addItem(new Water()); + meal.addItem(new Salad()); + return meal; + } + prepareNonVeganMeal() { + var meal = new Meal(); + meal.addItem(new BeefBurger()); + meal.addItem(new Coke()); + meal.addItem(new Fries()); + return meal; + } + prepareDeluxeMeal() { + var meal = new Meal(); + meal.addItem(new KobeBurger()); + meal.addItem(new Champagne()); + meal.addItem(new Crudettes()); + return meal; + } +} + +export default MealBuilder; \ No newline at end of file diff --git a/Builder/scripts/Packing.js b/Builder/scripts/Packing.js new file mode 100644 index 0000000..296f09a --- /dev/null +++ b/Builder/scripts/Packing.js @@ -0,0 +1,7 @@ +class Packing { + pack() { + throw new Error('This method should be implemented'); + } +} + +export default Packing; \ No newline at end of file diff --git a/Builder/scripts/items/Burger.js b/Builder/scripts/items/Burger.js new file mode 100644 index 0000000..7e285c6 --- /dev/null +++ b/Builder/scripts/items/Burger.js @@ -0,0 +1,10 @@ +import Wrapper from '../packing/Wrapper'; +import Item from '../Item'; + +class Burger extends Item { + get packing() { + return new Wrapper(); + } +} + +export default Burger; \ No newline at end of file diff --git a/Builder/scripts/items/Drink.js b/Builder/scripts/items/Drink.js new file mode 100644 index 0000000..66d0d12 --- /dev/null +++ b/Builder/scripts/items/Drink.js @@ -0,0 +1,10 @@ +import Bottle from '../packing/Bottle'; +import Item from '../Item'; + +class Drink extends Item { + get packing() { + return new Bottle(); + } +} + +export default Drink; \ No newline at end of file diff --git a/Builder/scripts/items/SideDishes.js b/Builder/scripts/items/SideDishes.js new file mode 100644 index 0000000..fffb74d --- /dev/null +++ b/Builder/scripts/items/SideDishes.js @@ -0,0 +1,10 @@ +import BoxUp from '../packing/BoxUp'; +import Item from '../Item'; + +class SideDishes extends Item { + get packing() { + return new BoxUp(); + } +} + +export default SideDishes; \ No newline at end of file diff --git a/Builder/scripts/items/burgers/BeefBurger.js b/Builder/scripts/items/burgers/BeefBurger.js new file mode 100644 index 0000000..6eadd5b --- /dev/null +++ b/Builder/scripts/items/burgers/BeefBurger.js @@ -0,0 +1,12 @@ +import Burger from '../Burger'; + +class BeefBurger extends Burger { + get price() { + return 13; + } + get name() { + return 'Beef Burger'; + } +} + +export default BeefBurger; \ No newline at end of file diff --git a/Builder/scripts/items/burgers/KobeBurger.js b/Builder/scripts/items/burgers/KobeBurger.js new file mode 100644 index 0000000..2c03a83 --- /dev/null +++ b/Builder/scripts/items/burgers/KobeBurger.js @@ -0,0 +1,12 @@ +import Burger from '../Burger'; + +class KobeBurger extends Burger { + get price() { + return 235; + } + get name() { + return 'Kobe Burger'; + } +} + +export default KobeBurger; \ No newline at end of file diff --git a/Builder/scripts/items/burgers/VeganBurger.js b/Builder/scripts/items/burgers/VeganBurger.js new file mode 100644 index 0000000..dba9433 --- /dev/null +++ b/Builder/scripts/items/burgers/VeganBurger.js @@ -0,0 +1,12 @@ +import Burger from '../Burger'; + +class VeganBurger extends Burger { + get price() { + return 16; + } + get name() { + return 'Vegan Burger'; + } +} + +export default VeganBurger; \ No newline at end of file diff --git a/Builder/scripts/items/drinks/Champagne.js b/Builder/scripts/items/drinks/Champagne.js new file mode 100644 index 0000000..46f402d --- /dev/null +++ b/Builder/scripts/items/drinks/Champagne.js @@ -0,0 +1,12 @@ +import Drink from '../Drink'; + +class Champagne extends Drink { + get price() { + return 90; + } + get name() { + return 'Champagne'; + } +} + +export default Champagne; \ No newline at end of file diff --git a/Builder/scripts/items/drinks/Coke.js b/Builder/scripts/items/drinks/Coke.js new file mode 100644 index 0000000..fceb436 --- /dev/null +++ b/Builder/scripts/items/drinks/Coke.js @@ -0,0 +1,12 @@ +import Drink from '../Drink'; + +class Coke extends Drink { + get price() { + return 3.5; + } + get name() { + return 'Coke'; + } +} + +export default Coke; \ No newline at end of file diff --git a/Builder/scripts/items/drinks/Water.js b/Builder/scripts/items/drinks/Water.js new file mode 100644 index 0000000..8b129a9 --- /dev/null +++ b/Builder/scripts/items/drinks/Water.js @@ -0,0 +1,12 @@ +import Drink from '../Drink'; + +class Water extends Drink { + get price() { + return 2.5; + } + get name() { + return 'Water'; + } +} + +export default Water; \ No newline at end of file diff --git a/Builder/scripts/items/side-dishes/Crudettes.js b/Builder/scripts/items/side-dishes/Crudettes.js new file mode 100644 index 0000000..2f666f2 --- /dev/null +++ b/Builder/scripts/items/side-dishes/Crudettes.js @@ -0,0 +1,12 @@ +import SideDishes from '../SideDishes'; + +class Crudettes extends SideDishes { + get price() { + return 4; + } + get name() { + return 'Crudettes'; + } +} + +export default Crudettes; \ No newline at end of file diff --git a/Builder/scripts/items/side-dishes/Fries.js b/Builder/scripts/items/side-dishes/Fries.js new file mode 100644 index 0000000..83d9bcc --- /dev/null +++ b/Builder/scripts/items/side-dishes/Fries.js @@ -0,0 +1,12 @@ +import SideDishes from '../SideDishes'; + +class Fries extends SideDishes { + get price() { + return 2; + } + get name() { + return 'Fries'; + } +} + +export default Fries; \ No newline at end of file diff --git a/Builder/scripts/items/side-dishes/Salad.js b/Builder/scripts/items/side-dishes/Salad.js new file mode 100644 index 0000000..0148b72 --- /dev/null +++ b/Builder/scripts/items/side-dishes/Salad.js @@ -0,0 +1,12 @@ +import SideDishes from '../SideDishes'; + +class Salad extends SideDishes { + get price() { + return 6; + } + get name() { + return 'Salad'; + } +} + +export default Salad; \ No newline at end of file diff --git a/Builder/scripts/main.js b/Builder/scripts/main.js new file mode 100644 index 0000000..735b76b --- /dev/null +++ b/Builder/scripts/main.js @@ -0,0 +1,19 @@ +import MealBuilder from './MealBuilder'; + +var mealBuilder = new MealBuilder(); + +var veganMeal = mealBuilder.prepareVeganMeal(); + +veganMeal.showItems(); +console.log(`Vegan meal cost: $${veganMeal.getCost()}`); + +var nonVeganMeal = mealBuilder.prepareNonVeganMeal(); + +nonVeganMeal.showItems(); +console.log(`Non vegan meal cost: $${nonVeganMeal.getCost()}`); + +var deluxeMeal = mealBuilder.prepareDeluxeMeal(); + +deluxeMeal.showItems(); +console.log(`Deluxe meal cost: $${deluxeMeal.getCost()}`); + diff --git a/Builder/scripts/packing/Bottle.js b/Builder/scripts/packing/Bottle.js new file mode 100644 index 0000000..35b2429 --- /dev/null +++ b/Builder/scripts/packing/Bottle.js @@ -0,0 +1,9 @@ +import Packing from '../Packing'; + +class Bottle extends Packing { + pack() { + return 'Bottle'; + } +} + +export default Bottle; \ No newline at end of file diff --git a/Builder/scripts/packing/BoxUp.js b/Builder/scripts/packing/BoxUp.js new file mode 100644 index 0000000..9c35634 --- /dev/null +++ b/Builder/scripts/packing/BoxUp.js @@ -0,0 +1,9 @@ +import Packing from '../Packing'; + +class BoxUp extends Packing { + pack() { + return 'Boxing'; + } +} + +export default BoxUp; \ No newline at end of file diff --git a/Builder/scripts/packing/Wrapper.js b/Builder/scripts/packing/Wrapper.js new file mode 100644 index 0000000..b0d1805 --- /dev/null +++ b/Builder/scripts/packing/Wrapper.js @@ -0,0 +1,9 @@ +import Packing from '../Packing'; + +class Wrapper extends Packing { + pack() { + return 'Wrapping'; + } +} + +export default Wrapper; \ No newline at end of file diff --git a/Factory/2/scripts/PizzaIngredientFactory.js b/Factory/2/scripts/PizzaIngredientFactory.js index 4686ca9..21470b6 100644 --- a/Factory/2/scripts/PizzaIngredientFactory.js +++ b/Factory/2/scripts/PizzaIngredientFactory.js @@ -11,10 +11,6 @@ class PizzaIngredientFactory { throw new Error("This method must be overwritten!"); } - createCheese() { - throw new Error("This method must be overwritten!"); - } - createVeggies() { throw new Error("This method must be overwritten!"); } diff --git a/Flyweight/index.html b/Flyweight/index.html new file mode 100644 index 0000000..a907275 --- /dev/null +++ b/Flyweight/index.html @@ -0,0 +1,49 @@ + + + + FlyWeight + + +
+

Source

+
+const CANVAS_SIZE = 600;
+const TREES_TO_DRAW = 99900;
+const TREE_TYPES = 2;
+const forest = new Forest();
+
+const getAmountOfTreesToRender = (amount, types) => {
+    return Math.floor(amount / types);
+};
+
+const random = (min, max) => {
+    return min + (Math.random() * ((max - min) + 1));
+}
+const renderForest = (forest, canvas) => {
+    for(let i = 0; i < getAmountOfTreesToRender(TREES_TO_DRAW, TREE_TYPES); i++) {
+        forest.plantTree(random(0, CANVAS_SIZE), random(0, CANVAS_SIZE), 'Red Maple', 'red', 'Red Maple texture');
+        forest.plantTree(random(0, CANVAS_SIZE), random(0, CANVAS_SIZE), 'Gray Birch', 'gray', 'Gray Birch texture stub');
+    }
+    
+    forest.render(canvas);
+    
+    console.log(TREES_TO_DRAW + ' trees rendered');
+    console.log('Memory usage:');
+    console.log('Tree size (8 bytes) * ' + TREES_TO_DRAW + '+ TreeTypes size (~30 bytes) * ' + TREE_TYPES);
+    console.log('Total: ' + ((TREES_TO_DRAW * 8 + TREE_TYPES * 30) / 1024  / 1024) + 'MB (instead of ' + ((TREES_TO_DRAW * 38) / 1024 / 1024) + 'MB)');
+}
+
+renderForest(new Forest(), document.createElement('canvas'));
+      
+
+
+

Console

+ +

FlyWeight

+
+ + + + + + diff --git a/Flyweight/scripts/Forest.js b/Flyweight/scripts/Forest.js new file mode 100644 index 0000000..448a2be --- /dev/null +++ b/Flyweight/scripts/Forest.js @@ -0,0 +1,24 @@ +import TreeFactory from './TreeFactory'; +import Tree from './Tree'; + +const privateTrees = new WeakMap(); +class Forest { + constructor() { + privateTrees.set(this, []); + } + get trees() { + return privateTrees.get(this); + } + plantTree(x, y, name, color, treeConfig) { + const type = TreeFactory.getTreeType(name, color, treeConfig); + const tree = new Tree(x, y, type); + this.trees.push(tree); + } + render(canvas) { + this.trees.forEach((tree) => { + tree.render(canvas); + }); + } +} + +export default Forest; \ No newline at end of file diff --git a/Flyweight/scripts/Tree.js b/Flyweight/scripts/Tree.js new file mode 100644 index 0000000..8f0d1a4 --- /dev/null +++ b/Flyweight/scripts/Tree.js @@ -0,0 +1,26 @@ +const privateX = new WeakMap(); +const privateY = new WeakMap(); +const privateTreeType = new WeakMap(); + +class Tree { + constructor(x = 0, y = 0, treeType) { + privateX.set(this, x); + privateY.set(this, y); + privateTreeType.set(this, treeType); + } + get x() { + return privateX.get(this); + } + get y() { + return privateY.get(this); + } + get treeType() { + return privateTreeType.get(this); + } + render(canvas) { + const context = canvas.getContext("2d"); + this.treeType.render(context, this.x, this.y); + } +} + +export default Tree; \ No newline at end of file diff --git a/Flyweight/scripts/TreeFactory.js b/Flyweight/scripts/TreeFactory.js new file mode 100644 index 0000000..cee33d0 --- /dev/null +++ b/Flyweight/scripts/TreeFactory.js @@ -0,0 +1,15 @@ +import TreeType from './TreeType'; + +const treeTypesMap = new Map(); +class TreeFactory { + static getTreeType(name, color, treeConfig) { + let result = treeTypesMap.get(name); + if(result == null) { + result = new TreeType(name, color, treeConfig); + treeTypesMap.set(name, result); + } + return result; + } +} + +export default TreeFactory; \ No newline at end of file diff --git a/Flyweight/scripts/TreeType.js b/Flyweight/scripts/TreeType.js new file mode 100644 index 0000000..c01a2d6 --- /dev/null +++ b/Flyweight/scripts/TreeType.js @@ -0,0 +1,26 @@ +const privateName = new WeakMap(); +const privateColor = new WeakMap(); +const privateTreeConfig = new WeakMap(); + +class TreeType { + constructor(name, color, treeConfig) { + privateName.set(this, name); + privateColor.set(this, color); + privateTreeConfig.set(this, treeConfig); + } + get name() { + return privateName.set(this); + } + get color() { + return privateColor.set(this); + } + get treeConfig() { + return privateTreeConfig.set(this); + } + render(context, x, y) { + context.fillStyle = "black"; + context.fillRect(x - 1, y, 3, 5); + } +} + +export default TreeType; \ No newline at end of file diff --git a/Flyweight/scripts/main.js b/Flyweight/scripts/main.js new file mode 100644 index 0000000..b5da4c2 --- /dev/null +++ b/Flyweight/scripts/main.js @@ -0,0 +1,29 @@ +import Forest from './Forest'; + +const CANVAS_SIZE = 600; +const TREES_TO_DRAW = 99900; +const TREE_TYPES = 2; +const forest = new Forest(); + +const getAmountOfTreesToRender = (amount, types) => { + return Math.floor(amount / types); +}; + +const random = (min, max) => { + return min + (Math.random() * ((max - min) + 1)); +} +const renderForest = (forest, canvas) => { + for(let i = 0; i < getAmountOfTreesToRender(TREES_TO_DRAW, TREE_TYPES); i++) { + forest.plantTree(random(0, CANVAS_SIZE), random(0, CANVAS_SIZE), 'Red Maple', 'red', 'Red Maple texture'); + forest.plantTree(random(0, CANVAS_SIZE), random(0, CANVAS_SIZE), 'Gray Birch', 'gray', 'Gray Birch texture stub'); + } + + forest.render(canvas); + + console.log(TREES_TO_DRAW + ' trees rendered'); + console.log('Memory usage:'); + console.log('Tree size (8 bytes) * ' + TREES_TO_DRAW + '+ TreeTypes size (~30 bytes) * ' + TREE_TYPES); + console.log('Total: ' + ((TREES_TO_DRAW * 8 + TREE_TYPES * 30) / 1024 / 1024) + 'MB (instead of ' + ((TREES_TO_DRAW * 38) / 1024 / 1024) + 'MB)'); +} + +renderForest(new Forest(), document.createElement('canvas')); \ No newline at end of file diff --git a/Prototype/index.html b/Prototype/index.html new file mode 100644 index 0000000..8057d9e --- /dev/null +++ b/Prototype/index.html @@ -0,0 +1,31 @@ + + + + Prototype Pattern + + +
+

Source

+
+import HumanBeing from './HumanBeing';
+
+var me = new HumanBeing({ skinColor: 'pale', hairColor: 'brown', height:'173cm', weight: '100kg', gender: 'male'});
+
+var clone = me.clone();
+
+console.log(`Are original and clone the same instance? ${me === clone}`);
+
+for(let key in me) {
+    console.log(`Are both ${key} property values in original and clone the same value? ${me[key] === clone[key]}`);
+}
+      
+
+
+

Console

+ +

Prototype

+
+ + + + diff --git a/Prototype/scripts/HumanBeing.js b/Prototype/scripts/HumanBeing.js new file mode 100644 index 0000000..ed22dd1 --- /dev/null +++ b/Prototype/scripts/HumanBeing.js @@ -0,0 +1,15 @@ +class HumanBeing { + constructor(config) { + this.skinColor = config.skinColor; + this.hairColor = config.hairColor; + this.height = config.height; + this.weight = config.weight; + this.gender = config.gender; + // And more data. + } + clone() { + return new HumanBeing(Object.assign({}, this)); + } +} + +export default HumanBeing; \ No newline at end of file diff --git a/Prototype/scripts/main.js b/Prototype/scripts/main.js new file mode 100644 index 0000000..5eb6c10 --- /dev/null +++ b/Prototype/scripts/main.js @@ -0,0 +1,11 @@ +import HumanBeing from './HumanBeing'; + +var me = new HumanBeing({ skinColor: 'pale', hairColor: 'brown', height:'173cm', weight: '100kg', gender: 'male'}); + +var clone = me.clone(); + +console.log(`Are original and clone the same instance? ${me === clone}`); + +for(let key in me) { + console.log(`Are both ${key} property values in original and clone the same value? ${me[key] === clone[key]}`); +} \ No newline at end of file diff --git a/README.md b/README.md index f774d9f..de06b37 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,9 @@ This will be a repository of how to use the Design Patterns from *Gang of Four* in your applications with Javascript. ## Update: +- Added Flyweight implementation. +- Added Bridge implementation. +- Added Prototype and Builder implementations. - All the Design Patterns have been refactored to ES6. - Added the Multi-Inheritance Design Patterns exclusive for ES6. - Added new Design Patterns exclusive from Javascript. @@ -11,6 +14,8 @@ This will be a repository of how to use the Design Patterns from *Gang of Four* ## Design Patterns that you can find in this repository: * Adapter +* Builder +* Bridge * Chaining * Command * Composite @@ -28,6 +33,7 @@ This will be a repository of how to use the Design Patterns from *Gang of Four* * Namespace * Nullify * Observer +* Prototype * Proxy * Singleton * State diff --git a/Singleton/1/scripts/main.js b/Singleton/1/scripts/main.js index 1b28339..e52b2ab 100644 --- a/Singleton/1/scripts/main.js +++ b/Singleton/1/scripts/main.js @@ -3,4 +3,4 @@ import Singleton from './Singleton'; var oSingle1 = Singleton; var oSingle2 = Singleton; console.log(Singleton.toString()); -console.log("oSingle1 is the same instance that oSingle2? " + (oSingle1 === oSingle2)); +console.log("Are oSingle1 and oSingle2 the same instance? " + (oSingle1 === oSingle2)); diff --git a/Singleton/2/index.html b/Singleton/2/index.html index 4d862de..9b78346 100644 --- a/Singleton/2/index.html +++ b/Singleton/2/index.html @@ -12,7 +12,7 @@

Source

var oSingle1 = new Singleton(); var oSingle2 = new Singleton(); console.log(new Singleton().toString()); -console.log("oSingle1 is the same instance that oSingle2? " + (oSingle1 === oSingle2)); +console.log("Are oSingle1 and oSingle2 the same instance? " + (oSingle1 === oSingle2));
diff --git a/Singleton/2/scripts/main.js b/Singleton/2/scripts/main.js index 71a60d0..3611998 100644 --- a/Singleton/2/scripts/main.js +++ b/Singleton/2/scripts/main.js @@ -3,4 +3,4 @@ import Singleton from './Singleton'; var oSingle1 = new Singleton(); var oSingle2 = new Singleton(); console.log(new Singleton().toString()); -console.log("oSingle1 is the same instance that oSingle2? " + (oSingle1 === oSingle2)); +console.log("Are oSingle1 and oSingle2 the same instance? " + (oSingle1 === oSingle2)); diff --git a/Singleton/3/index.html b/Singleton/3/index.html index ea9e001..110aadb 100644 --- a/Singleton/3/index.html +++ b/Singleton/3/index.html @@ -12,7 +12,7 @@

Source

var oSingle1 = Singleton; var oSingle2 = Singleton; console.log(Singleton.toString()); -console.log("oSingle1 is the same instance that oSingle2? " + (oSingle1 === oSingle2)); +console.log("Are oSingle1 and oSingle2 the same instance? " + (oSingle1 === oSingle2));
diff --git a/Singleton/4/index.html b/Singleton/4/index.html index f535326..b1a6ffa 100644 --- a/Singleton/4/index.html +++ b/Singleton/4/index.html @@ -12,7 +12,7 @@

Source

var oSingle1 = Singleton.getInstance(); var oSingle2 = Singleton.getInstance(); console.log(Singleton.getInstance().toString()); -console.log("oSingle1 is the same instance that oSingle2? " + (oSingle1 === oSingle2)); +console.log("Are oSingle1 and oSingle2 the same instance? " + (oSingle1 === oSingle2));
diff --git a/Singleton/4/scripts/main.js b/Singleton/4/scripts/main.js index 732239c..c0191e3 100644 --- a/Singleton/4/scripts/main.js +++ b/Singleton/4/scripts/main.js @@ -3,4 +3,4 @@ import Singleton from './Singleton'; var oSingle1 = Singleton.getInstance(); var oSingle2 = Singleton.getInstance(); console.log(Singleton.getInstance().toString()); -console.log("oSingle1 is the same instance that oSingle2? " + (oSingle1 === oSingle2)); +console.log("Are oSingle1 and oSingle2 the same instance? " + (oSingle1 === oSingle2)); diff --git a/Singleton/5/index.html b/Singleton/5/index.html new file mode 100644 index 0000000..c7d4c47 --- /dev/null +++ b/Singleton/5/index.html @@ -0,0 +1,25 @@ + + + + Singleton Pattern + + +
+

Source

+
+import DatabaseConnection from './DatabaseConnection';
+
+var oSingle1 = DatabaseConnection.instance;
+var oSingle2 = DatabaseConnection.instance;
+console.log("Are oSingle1 and oSingle2 the same instance? " + (oSingle1 === oSingle2));        
+      
+
+
+

Console

+ +

SINGLETON

+
+ + + + diff --git a/Singleton/5/scripts/DatabaseConnection.js b/Singleton/5/scripts/DatabaseConnection.js new file mode 100644 index 0000000..46aa53b --- /dev/null +++ b/Singleton/5/scripts/DatabaseConnection.js @@ -0,0 +1,24 @@ +let DBCInstance = null; +global.DatabaseConnection = class DatabaseConnection { + get url() { + return 'mongodb://localhost:27017/myproject'; + } + get username() { + return 'admin'; + } + get connection() { + let connection; + // Do something to get the connection to the DB. + return connection; + } + get password() { + return 'localhost'; + } + static get instance() { + if(DBCInstance === null || + DBCInstance.getConnection().isClosed()) { + DBCInstance = new DatabaseConnection(); + } + return DBCInstance; + } +} \ No newline at end of file diff --git a/Singleton/5/scripts/main.js b/Singleton/5/scripts/main.js new file mode 100644 index 0000000..0fdfe40 --- /dev/null +++ b/Singleton/5/scripts/main.js @@ -0,0 +1,5 @@ +import DatabaseConnection from './DatabaseConnection'; + +var oSingle1 = DatabaseConnection.instance; +var oSingle2 = DatabaseConnection.instance; +console.log("Are oSingle1 and oSingle2 the same instance? " + (oSingle1 === oSingle2));