-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample06.ts
More file actions
53 lines (43 loc) · 1.67 KB
/
example06.ts
File metadata and controls
53 lines (43 loc) · 1.67 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
interface OldSystemInterface {
legacyOperation(): string;
}
interface NewSystemInterface {
modernOperation(): string;
}
class OldSystem implements OldSystemInterface {
legacyOperation(): string {
return "Виконую стару операцію";
}
}
class NewSystem implements NewSystemInterface {
modernOperation(): string {
return "Виконую нову операцію";
}
}
// Двосторонній адаптер
class BidirectionalAdapter implements OldSystemInterface, NewSystemInterface {
private oldSystem: OldSystemInterface;
private newSystem: NewSystemInterface;
constructor(oldSystem: OldSystemInterface, newSystem: NewSystemInterface) {
this.oldSystem = oldSystem;
this.newSystem = newSystem;
}
// Реалізація інтерфейсу OldSystemInterface
legacyOperation(): string {
console.log("Адаптер перенаправляє виклик до старої системи");
return this.oldSystem.legacyOperation();
}
// Реалізація інтерфейсу NewSystemInterface
modernOperation(): string {
console.log("Адаптер перенаправляє виклик до нової системи");
return this.newSystem.modernOperation();
}
}
// Використання
const oldSystem = new OldSystem();
const newSystem = new NewSystem();
const adapter = new BidirectionalAdapter(oldSystem, newSystem);
// Використання адаптера з інтерфейсом старої системи
console.log(adapter.legacyOperation());
// Використання адаптера з інтерфейсом нової системи
console.log(adapter.modernOperation());