-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
96 lines (80 loc) · 2.58 KB
/
proxy.ts
File metadata and controls
96 lines (80 loc) · 2.58 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
/**
* proxy.ts
* Proxy Pattern
* A Structural Pattern
* Used to control access to an object (security, lazy loading etc.)
* The software equivalent of a bouncer, middleman, or stunt double. It’s not the real thing,
* but it stands in front of the real thing, controlling access to it.
* Proxy is a structural design pattern that lets you provide a substitute or placeholder for another object.
* A proxy controls access to the original object, allowing you to perform something either before or after the request
* gets through to the original object.
* standIn Patterns
* Proxies give more control over accesing an object.
* Using the stand in allows you to perform other work in input and output prior to getting to the object
* Uses:
* Access Control (Protection Proxy),
* Lazy Initialization (Virtual Proxy),
* Remote Proxy,
* Caching Proxy,
* Logging / Monitoring Proxy,
* Smart Reference Proxy,
*/
/**
* The Target interface shows what is in common between the actual object and the standIn
*/
// TODO: Refactor interface into interface.ts file
interface Target {
request(): void;
}
/**
* The ActualObject does some logic
*/
class ActualObject implements Target {
public request(): void {
console.log('ActualObject doing some request');
}
}
/**
* The standIn is a proxy for the ActualObject and shares the interface with the ActualObject
*/
class StandIn implements Target {
private actualObject: ActualObject;
/**
* The standIn needs a reference to the actual object
*/
constructor(actualObject:ActualObject) {
this.actualObject = actualObject;
}
/**
* Perform results method on behalf of the actual object
*/
public request(): void {
if (this.checkData()) {
this.actualObject.request();
this.logRequest();
}
}
private checkData(): boolean {
// some checks of data validation should go here
console.log('standIn: validating data prior to sending request to actual object');
return true;
}
private logRequest():void {
console.log('standIn: Logging timing of request being sent');
}
}
/**
* The clientCode allows interaction with both the proxy and the actual object
*/
function clientCode(target:Target) {
// ...
target.request();
// ...
}
console.log('Client: Executing the client code on the actual object: ');
const actualObject = new ActualObject();
clientCode(actualObject);
console.log('');
console.log('Client: Executing the client code on the proxy: ');
const standIn = new StandIn(actualObject);
clientCode(standIn);