-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabstract_factory.ts
More file actions
173 lines (137 loc) · 4.24 KB
/
abstract_factory.ts
File metadata and controls
173 lines (137 loc) · 4.24 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Інтерфейс для стільця
interface Chair {
sitOn(): void;
hasLegs(): boolean;
getStyle(): string;
}
// Інтерфейс для столу
interface Table {
putOn(item: string): void;
getStyle(): string;
getNumberOfLegs(): number;
}
// Інтерфейс для дивана
interface Sofa {
lieOn(): void;
getSeatingCapacity(): number;
getStyle(): string;
}
// 2. КОНКРЕТНІ ПРОДУКТИ
// Сучасні меблі
class ModernChair implements Chair {
sitOn(): void {
console.log("Сидіння на сучасному стільці: зручно, мінімалістично");
}
hasLegs(): boolean {
return true; // Більшість сучасних стільців мають ніжки
}
getStyle(): string {
return "сучасний";
}
}
class ModernTable implements Table {
putOn(item: string): void {
console.log(`Розміщення ${item} на сучасному столі зі скляною поверхнею`);
}
getStyle(): string {
return "сучасний";
}
getNumberOfLegs(): number {
return 4;
}
}
class ModernSofa implements Sofa {
lieOn(): void {
console.log("Лежання на сучасному дивані: комфортно, низько до підлоги");
}
getSeatingCapacity(): number {
return 3;
}
getStyle(): string {
return "сучасний";
}
}
// Вікторіанські меблі
class VictorianChair implements Chair {
sitOn(): void {
console.log("Сидіння на вікторіанському стільці: вишукано, з прямою спинкою");
}
hasLegs(): boolean {
return true; // Вікторіанські стільці завжди мають ніжки
}
getStyle(): string {
return "вікторіанський";
}
}
class VictorianTable implements Table {
putOn(item: string): void {
console.log(`Розміщення ${item} на вікторіанському столі з дерев'яною різьбленою поверхнею`);
}
getStyle(): string {
return "вікторіанський";
}
getNumberOfLegs(): number {
return 4;
}
}
class VictorianSofa implements Sofa {
lieOn(): void {
console.log("Лежання на вікторіанському дивані: елегантно, з високими підлокітниками");
}
getSeatingCapacity(): number {
return 2; // Вікторіанські дивани часто менші
}
getStyle(): string {
return "вікторіанський";
}
}
// 3. АБСТРАКТНА ФАБРИКА
interface FurnitureFactory {
createChair(): Chair;
createTable(): Table;
createSofa(): Sofa;
}
// 4. КОНКРЕТНІ ФАБРИКИ
class ModernFurnitureFactory implements FurnitureFactory {
createChair(): Chair {
return new ModernChair();
}
createTable(): Table {
return new ModernTable();
}
createSofa(): Sofa {
return new ModernSofa();
}
}
class VictorianFurnitureFactory implements FurnitureFactory {
createChair(): Chair {
return new VictorianChair();
}
createTable(): Table {
return new VictorianTable();
}
createSofa(): Sofa {
return new VictorianSofa();
}
}
// 5. КЛІЄНТСЬКИЙ КОД
function furnishLivingRoom(factory: FurnitureFactory): void {
console.log("Обладнання вітальні...");
// Створення меблів за допомогою фабрики
const chair = factory.createChair();
const table = factory.createTable();
const sofa = factory.createSofa();
// Використання створених меблів
console.log(`Стиль меблів: ${chair.getStyle()}`);
chair.sitOn();
table.putOn("ваза з квітами");
sofa.lieOn();
console.log(`Диван вміщує ${sofa.getSeatingCapacity()} осіб`);
console.log(`Стіл має ${table.getNumberOfLegs()} ніжки`);
console.log("------------------------");
}
// Використання
console.log("Створення сучасної вітальні:");
furnishLivingRoom(new ModernFurnitureFactory());
console.log("\nСтворення вікторіанської вітальні:");
furnishLivingRoom(new VictorianFurnitureFactory());