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
| interface Subject { request(): void; }
class RealSubject implements Subject { public request(): void { console.log('RealSubject: Handling request.'); } } class Proxy 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 clientCode(subject: Subject) {
subject.request();
} const realSubject = new RealSubject(); const proxy = new Proxy(realSubject); clientCode(proxy);
|