-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory_method.ts
More file actions
113 lines (91 loc) · 4.1 KB
/
factory_method.ts
File metadata and controls
113 lines (91 loc) · 4.1 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
// Інтерфейс Transport або продукт (Product)
// Визначає контракт, якому повинні відповідати всі конкретні продукти
interface Transport {
deliver(): void;
calculateCost(distance: number): number;
getEstimatedTime(distance: number): number;
}
// Конкретний продукт #1: Наземний транспорт
class Truck implements Transport {
deliver(): void {
console.log("Доставка вантажу автомобілем по дорозі");
}
calculateCost(distance: number): number {
return distance * 2.5; // 2.5 грн/км
}
getEstimatedTime(distance: number): number {
return Math.ceil(distance / 80); // 80 км/год в середньому
}
}
// Конкретний продукт #2: Морський транспорт
class Ship implements Transport {
deliver(): void {
console.log("Доставка вантажу кораблем по морю");
}
calculateCost(distance: number): number {
return distance * 1.8 + 200; // Базова вартість + 1.8 грн/км
}
getEstimatedTime(distance: number): number {
return Math.ceil(distance / 40); // 40 км/год в середньому
}
}
// Конкретний продукт #3: Повітряний транспорт
class Plane implements Transport {
deliver(): void {
console.log("Доставка вантажу літаком по повітрю");
}
calculateCost(distance: number): number {
return distance * 6.5 + 1500; // Висока базова вартість + 6.5 грн/км
}
getEstimatedTime(distance: number): number {
return Math.ceil(distance / 800); // 800 км/год в середньому
}
}
// Creator - абстрактний клас, який оголошує фабричний метод для створення продуктів
abstract class Logistics {
abstract createTransport(): Transport;
planDelivery(distance: number): void {
// Створення продукту через фабричний метод
const transport = this.createTransport();
console.log("=== Планування доставки ===");
transport.deliver();
const cost = transport.calculateCost(distance);
console.log(`Вартість: ${cost} грн`);
const time = transport.getEstimatedTime(distance);
console.log(`Орієнтовний час: ${time} годин`);
console.log("===========================");
}
}
// Конкретний творець #1: Створює наземний транспорт
class RoadLogistics extends Logistics {
// Реалізація фабричного методу для створення об'єктів Truck
createTransport(): Transport {
return new Truck();
}
}
// Конкретний творець #2: Створює морський транспорт
class SeaLogistics extends Logistics {
// Реалізація фабричного методу для створення об'єктів Ship
createTransport(): Transport {
return new Ship();
}
}
// Конкретний творець #3: Створює повітряний транспорт
class AirLogistics extends Logistics {
// Реалізація фабричного методу для створення об'єктів Plane
createTransport(): Transport {
return new Plane();
}
}
// Клієнтський код, який використовує патерн
function clientCode(logistics: Logistics, distance: number) {
// Клієнт працює з абстракцією Logistics і не знає конкретних класів
logistics.planDelivery(distance);
}
// Використання різних типів логістики
console.log("Використанно тип логістики RoadLogistics");
clientCode(new RoadLogistics(), 100);
console.log("\nВикористанно тип логістики SeaLogistics");
clientCode(new SeaLogistics(), 1000);
console.log("\nВикористанно тип логістики AirLogistics");
clientCode(new AirLogistics(), 2000);