Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions practice/2021/hayk_ohanyan/AbstractFactory/Bmw/Factory.class.js
Original file line number Diff line number Diff line change
@@ -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;
18 changes: 18 additions & 0 deletions practice/2021/hayk_ohanyan/AbstractFactory/Bmw/Models/X.class.js
Original file line number Diff line number Diff line change
@@ -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;
19 changes: 19 additions & 0 deletions practice/2021/hayk_ohanyan/AbstractFactory/CarFactory.class.js
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
18 changes: 18 additions & 0 deletions practice/2021/hayk_ohanyan/AbstractFactory/index.js
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
15 changes: 15 additions & 0 deletions practice/2021/hayk_ohanyan/FactoryMethod/index.js
Original file line number Diff line number Diff line change
@@ -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);
}
19 changes: 19 additions & 0 deletions practice/2021/hayk_ohanyan/PatternFactory/Factory.class.js
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 10 additions & 0 deletions practice/2021/hayk_ohanyan/PatternFactory/Gadget/Tablet.class.js
Original file line number Diff line number Diff line change
@@ -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;
24 changes: 24 additions & 0 deletions practice/2021/hayk_ohanyan/PatternFactory/index.js
Original file line number Diff line number Diff line change
@@ -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);