-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
facade-concept.ts
52 lines (44 loc) · 1.32 KB
/
facade-concept.ts
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
// The Facade pattern concept
class SubSystemClassA {
// A hypothetically complicated class
method(): string {
return 'A'
}
}
class SubSystemClassB {
// A hypothetically complicated class
method(value: string): string {
return value
}
}
class SubSystemClassC {
// A hypothetically complicated class
method(value: {C: number[]}): {C: number[]} {
return value
}
}
class Facade {
// A simplified facade offering the services of subsystems
subSystemClassA(): string {
// Uses the subsystems method
return new SubSystemClassA().method()
}
subSystemClassB(value: string): string {
// Uses the subsystems method
return new SubSystemClassB().method(value)
}
subSystemClassC(value: {C: number[]}): {C: number[]} {
// Uses the subsystems method
return new SubSystemClassC().method(value)
}
}
// The Client
// Calling potentially complicated subsystems directly
console.log(new SubSystemClassA().method())
console.log(new SubSystemClassB().method('B'))
console.log(new SubSystemClassC().method({ C: [1, 2, 3] }))
// or using the simplified facade instead
const FACADE = new Facade()
console.log(FACADE.subSystemClassA())
console.log(FACADE.subSystemClassB('B'))
console.log(FACADE.subSystemClassC({ C: [1, 2, 3] }))