-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractFactory2.js
More file actions
58 lines (50 loc) · 1.49 KB
/
AbstractFactory2.js
File metadata and controls
58 lines (50 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Abstract Factory is a creational pattern.
// Абстрактная фабрика - порождающий шаблон проектирования.
// Abstract Factory (Абстрактная фабрика)
class factoryChocolate {
// Factories (Фабрики)
milk() {
return new CreateChocolateMilk('sweet');
}
dark() {
return new CreateChocolateDark('горький');
}
}
// Base class (Базовый класс)
class CreateChocolate {
constructor(type) {
this.chocolateType = type;
}
info() {
console.log(`This is a ${this.chocolateType} chocolate`);
}
}
// Inherit and extend if necessary
// Наследуем и расширяем, если необходимо
class CreateChocolateMilk extends CreateChocolate {
constructor(...args) {
super(...args);
}
}
// Inherit and extend if necessary
// Наследуем и расширяем, если необходимо
class CreateChocolateDark extends CreateChocolate {
constructor(...args) {
super(...args);
}
info() {
// Override method (Переопределяем метод)
console.log(`Это ${this.chocolateType} шоколад`);
}
}
const chocolateCreate1 = new factoryChocolate();
const chocolateCreate2 = new factoryChocolate();
// Instances (Экземпляры)
const milk1 = chocolateCreate1.milk();
const dark1 = chocolateCreate1.dark();
const milk2 = chocolateCreate2.milk();
const dark2 = chocolateCreate2.dark();
milk1.info();
dark1.info();
milk2.info();
dark2.info();