diff --git a/practice/2021/hayk_ohanyan/AbstractFactory/Bmw/Factory.class.js b/practice/2021/hayk_ohanyan/AbstractFactory/Bmw/Factory.class.js new file mode 100644 index 0000000..de531db --- /dev/null +++ b/practice/2021/hayk_ohanyan/AbstractFactory/Bmw/Factory.class.js @@ -0,0 +1,18 @@ +const X = require('./Models/X.class'); + +class BmwFactory { + static models = { + x5: new X('x5', 300, '$108,000', 2020), + x6: new X('x6', 320, '$111,000', 2020) + } + + create(model) { + if (!BmwFactory.models[model]) { + throw new Error(`Bmw model ${model} not found :(`); + } + + return BmwFactory.models[model]; + } +} + +module.exports = BmwFactory; diff --git a/practice/2021/hayk_ohanyan/AbstractFactory/Bmw/Models/X.class.js b/practice/2021/hayk_ohanyan/AbstractFactory/Bmw/Models/X.class.js new file mode 100644 index 0000000..10c8550 --- /dev/null +++ b/practice/2021/hayk_ohanyan/AbstractFactory/Bmw/Models/X.class.js @@ -0,0 +1,18 @@ +class X { + constructor(model, maxSpeed, price, year) { + this.model = model; + this.year = year; + this.price = price; + this.maxSpeed = maxSpeed; + } + + info() { + console.log('=== Bmw X ==='); + console.log(`Model: ${this.model}`); + console.log(`Year: ${this.year}`); + console.log(`Max speed: ${this.maxSpeed}`); + console.log(`Price: ${this.price}`, '\n'); + } +} + +module.exports = X; diff --git a/practice/2021/hayk_ohanyan/AbstractFactory/CarFactory.class.js b/practice/2021/hayk_ohanyan/AbstractFactory/CarFactory.class.js new file mode 100644 index 0000000..9b2c60b --- /dev/null +++ b/practice/2021/hayk_ohanyan/AbstractFactory/CarFactory.class.js @@ -0,0 +1,19 @@ +const MercedesFactory = require('./Mercedes/Factory.class'); +const BmwFactory = require('./Bmw/Factory.class'); + +class CarFactory { + static cars = { + mercedes: MercedesFactory, + bmw: BmwFactory + } + + create(type, model) { + if (!CarFactory.cars[type]) { + throw new Error(`${type.toUpperCase()} car not found :(`); + } + + return CarFactory.cars[type].models[model]; + } +} + +module.exports = CarFactory; diff --git a/practice/2021/hayk_ohanyan/AbstractFactory/Mercedes/Factory.class.js b/practice/2021/hayk_ohanyan/AbstractFactory/Mercedes/Factory.class.js new file mode 100644 index 0000000..d5b11f5 --- /dev/null +++ b/practice/2021/hayk_ohanyan/AbstractFactory/Mercedes/Factory.class.js @@ -0,0 +1,21 @@ +const C = require('./Models/C.class'); +const E = require('./Models/E.class'); + +class MercedesFactory { + static models = { + c300: new C('c300', 250, '$41,600', 2021), + c200: new C('c200', 234, '$37,500', 2021), + c180: new C('c300', 223, '$25,000', 2019), + e320: new E('e320', 245, '$55,300', 2020) + } + + create(model) { + if (!MercedesFactory.models[model]) { + throw new Error(`Mercedes model ${model} not found :(`); + } + + return MercedesFactory.models[model]; + } +} + +module.exports = MercedesFactory; diff --git a/practice/2021/hayk_ohanyan/AbstractFactory/Mercedes/Models/C.class.js b/practice/2021/hayk_ohanyan/AbstractFactory/Mercedes/Models/C.class.js new file mode 100644 index 0000000..8afcc58 --- /dev/null +++ b/practice/2021/hayk_ohanyan/AbstractFactory/Mercedes/Models/C.class.js @@ -0,0 +1,18 @@ +class C { + constructor(model, maxSpeed, price, year) { + this.model = model; + this.year = year; + this.price = price; + this.maxSpeed = maxSpeed; + } + + info() { + console.log('=== Mercedes C class ==='); + console.log(`Model: ${this.model}`); + console.log(`Year: ${this.year}`); + console.log(`Max speed: ${this.maxSpeed}`); + console.log(`Price: ${this.price}`, '\n'); + } +} + +module.exports = C; diff --git a/practice/2021/hayk_ohanyan/AbstractFactory/Mercedes/Models/E.class.js b/practice/2021/hayk_ohanyan/AbstractFactory/Mercedes/Models/E.class.js new file mode 100644 index 0000000..92d0a23 --- /dev/null +++ b/practice/2021/hayk_ohanyan/AbstractFactory/Mercedes/Models/E.class.js @@ -0,0 +1,18 @@ +class E { + constructor(model, maxSpeed, price, year) { + this.model = model; + this.year = year; + this.price = price; + this.maxSpeed = maxSpeed; + } + + info() { + console.log('=== Mercedes E class ==='); + console.log(`Model: ${this.model}`); + console.log(`Year: ${this.year}`); + console.log(`Max speed: ${this.maxSpeed}`); + console.log(`Price: ${this.price}`, '\n'); + } +} + +module.exports = E; diff --git a/practice/2021/hayk_ohanyan/AbstractFactory/index.js b/practice/2021/hayk_ohanyan/AbstractFactory/index.js new file mode 100644 index 0000000..b1e2f5a --- /dev/null +++ b/practice/2021/hayk_ohanyan/AbstractFactory/index.js @@ -0,0 +1,18 @@ +const CarFactory = require('./CarFactory.class'); + +const factory = new CarFactory(); + +try { + const cars = [ + factory.create('bmw', 'x5'), + factory.create('mercedes', 'c300'), + factory.create('mercedes', 'e320'), + factory.create('mercedes', 'c200'), + factory.create('bmw', 'x6'), + factory.create('mercedes', 'c180') + ]; + + cars.forEach(car => car.info()); +} catch (e) { + console.log(e); +} diff --git a/practice/2021/hayk_ohanyan/FactoryMethod/MercedesCars/Factory.class.js b/practice/2021/hayk_ohanyan/FactoryMethod/MercedesCars/Factory.class.js new file mode 100644 index 0000000..1b86080 --- /dev/null +++ b/practice/2021/hayk_ohanyan/FactoryMethod/MercedesCars/Factory.class.js @@ -0,0 +1,19 @@ +const C = require('./Models/C.class'); + +class MercedesFactory { + static models = { + c300: new C('c300', 250, '$41,600', 2021), + c200: new C('c200', 234, '$37,500', 2021), + c180: new C('c300', 223, '$25,000', 2019) + } + + create(model) { + if (!MercedesFactory.models[model]) { + throw new Error(`Model ${model} not found :(`); + } + + return MercedesFactory.models[model]; + } +} + +module.exports = MercedesFactory; diff --git a/practice/2021/hayk_ohanyan/FactoryMethod/MercedesCars/Models/C.class.js b/practice/2021/hayk_ohanyan/FactoryMethod/MercedesCars/Models/C.class.js new file mode 100644 index 0000000..8f6765d --- /dev/null +++ b/practice/2021/hayk_ohanyan/FactoryMethod/MercedesCars/Models/C.class.js @@ -0,0 +1,17 @@ +class C { + constructor(model, maxSpeed, price, year) { + this.model = model; + this.year = year; + this.price = price; + this.maxSpeed = maxSpeed; + } + + info() { + console.log(`Model: ${this.model}`); + console.log(`Year: ${this.year}`); + console.log(`Max speed: ${this.maxSpeed}`); + console.log(`Price: ${this.price}`, '\n'); + } +} + +module.exports = C; diff --git a/practice/2021/hayk_ohanyan/FactoryMethod/index.js b/practice/2021/hayk_ohanyan/FactoryMethod/index.js new file mode 100644 index 0000000..2a9681c --- /dev/null +++ b/practice/2021/hayk_ohanyan/FactoryMethod/index.js @@ -0,0 +1,15 @@ +const MercedesFactory = require('./MercedesCars/Factory.class'); + +const factory = new MercedesFactory(); + +try { + const cars = [ + factory.create('c300'), + factory.create('c180'), + factory.create('c200'), + ]; + + cars.forEach(car => car.info()); +} catch (e) { + console.log(e); +} diff --git a/practice/2021/hayk_ohanyan/PatternFactory/Factory.class.js b/practice/2021/hayk_ohanyan/PatternFactory/Factory.class.js new file mode 100644 index 0000000..87261eb --- /dev/null +++ b/practice/2021/hayk_ohanyan/PatternFactory/Factory.class.js @@ -0,0 +1,19 @@ +const Laptop = require('./Gadget/Laptop.class'); +const Tablet = require('./Gadget/Tablet.class'); +const SmartPhone = require('./Gadget/SmartPhone.class'); + +class GadgetFactory { + static gadgets = { + Laptop, + Tablet, + SmartPhone + } + + create(type, options) { + const Gadget = GadgetFactory.gadgets[type]; + + return new Gadget(options); + } +} + +module.exports = GadgetFactory; diff --git a/practice/2021/hayk_ohanyan/PatternFactory/Gadget/Laptop.class.js b/practice/2021/hayk_ohanyan/PatternFactory/Gadget/Laptop.class.js new file mode 100644 index 0000000..e0bba76 --- /dev/null +++ b/practice/2021/hayk_ohanyan/PatternFactory/Gadget/Laptop.class.js @@ -0,0 +1,9 @@ +class Laptop { + constructor(options) { + this.name = options?.name || ''; + this.hdd = options?.hdd || 0; + this.ram = options?.ram || 0; + } +} + +module.exports = Laptop; diff --git a/practice/2021/hayk_ohanyan/PatternFactory/Gadget/SmartPhone.class.js b/practice/2021/hayk_ohanyan/PatternFactory/Gadget/SmartPhone.class.js new file mode 100644 index 0000000..89ce96e --- /dev/null +++ b/practice/2021/hayk_ohanyan/PatternFactory/Gadget/SmartPhone.class.js @@ -0,0 +1,11 @@ +class SmartPhone { + constructor(options) { + this.name = options?.name || ''; + this.hdd = options?.hdd || 0; + this.ram = options?.ram || 0; + this.network = options?.network || '4g'; + this.color = options?.color || 'black'; + } +} + +module.exports = SmartPhone; diff --git a/practice/2021/hayk_ohanyan/PatternFactory/Gadget/Tablet.class.js b/practice/2021/hayk_ohanyan/PatternFactory/Gadget/Tablet.class.js new file mode 100644 index 0000000..6b72927 --- /dev/null +++ b/practice/2021/hayk_ohanyan/PatternFactory/Gadget/Tablet.class.js @@ -0,0 +1,10 @@ +class Tablet { + constructor(options) { + this.name = options?.name || ''; + this.hdd = options?.hdd || 0; + this.ram = options?.ram || 0; + this.network = options?.network || '5g'; + } +} + +module.exports = Tablet; diff --git a/practice/2021/hayk_ohanyan/PatternFactory/index.js b/practice/2021/hayk_ohanyan/PatternFactory/index.js new file mode 100644 index 0000000..acab3c2 --- /dev/null +++ b/practice/2021/hayk_ohanyan/PatternFactory/index.js @@ -0,0 +1,24 @@ +const GadgetFactory = require('./Factory.class') + +const factory = new GadgetFactory() + +const gadgets = [ + factory.create('Tablet', { + name: 'Ipad 4', + ram: 8, + hdd: 256, + network: '4g' + }), + factory.create('Laptop', { + hdd: 1000, + ram: 16, + name: 'Asus VivoBook' + }), + factory.create('SmartPhone', { + hdd: 64, + ram: 4, + name: 'HTC' + }) +] + +console.log(gadgets);