-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
52 lines (39 loc) · 1.03 KB
/
index.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
interface Subject {
request(): void;
}
class RealSubject implements Subject {
public request(): void {
console.log('RealSubject: Handling request.');
}
}
class ProxyPattern implements Subject {
private realSubject: RealSubject
constructor(realsubject: RealSubject) {
this.realSubject = realsubject
}
public request(): void {
if (this.checkAccess()) {
this.realSubject.request()
this.logAccess()
}
}
private checkAccess(): Boolean {
console.log('Proxy: Checking access prior to firing a real request.');
return true;
}
private logAccess(): void {
console.log('Proxy: Logging the time of request.');
}
}
function clientCodeContext() {
console.log('Client: Executing the client code with a real subject:');
const realSubject = new RealSubject();
realSubject.request()
console.log('');
console.log('Client: Executing the same client code with a proxy:');
const proxy = new ProxyPattern(realSubject);
proxy.request()
}
export function proxy() {
clientCodeContext()
}