In this article, we will learn how to use and implement the Proxy Pattern in TypeScript with an example.
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.
TypeScript Proxy Pattern Example
The below diagram shows the generic structure of the Proxy Pattern:
Let's refer to the above structure to create an example to demonstrates the usage of the Proxy Pattern.
Let's create proxy.ts file and add the following code to it:
export interface Subject {
doAction(): void;
}
export class Proxy implements Subject {
private realSubject: RealSubject;
private s: string;
constructor(s: string) {
this.s = s;
}
public doAction(): void {
console.log("`doAction` of Proxy(", this.s, ")");
if (this.realSubject === null || this.realSubject === undefined) {
console.log("creating a new RealSubject.");
this.realSubject = new RealSubject(this.s);
}
this.realSubject.doAction();
}
}
export class RealSubject implements Subject {
private s: string;
constructor(s: string) {
this.s = s;
}
public doAction(): void {
console.log("`doAction` of RealSubject", this.s, "is being called!");
}
}
Usage
Let's create demo.ts file and add the following code to it:
import { Proxy } from "./proxy";
export function show() : void {
var proxy1: Proxy = new Proxy("proxy1"),
proxy2: Proxy = new Proxy("proxy2");
proxy1.doAction();
proxy1.doAction();
proxy2.doAction();
proxy2.doAction();
proxy1.doAction();
}
show();
Run:
- Compile the above code using the TypeScript compiler.
- Above code is compiled to plan JavaScript code
- Run Javascript code using node
design_patterns_in_typescript\proxy> tsc --target ES5 .\demo.ts
design_patterns_in_typescript\proxy> node .\demo.js
`doAction` of Proxy( proxy1 )
creating a new RealSubject.
`doAction` of RealSubject proxy1 is being called!
`doAction` of Proxy( proxy1 )
`doAction` of RealSubject proxy1 is being called!
`doAction` of Proxy( proxy2 )
creating a new RealSubject.
`doAction` of RealSubject proxy2 is being called!
`doAction` of RealSubject proxy2 is being called!
`doAction` of Proxy( proxy2 )
`doAction` of RealSubject proxy2 is being called!
`doAction` of Proxy( proxy1 )
`doAction` of RealSubject proxy1 is being called!
All TypeScript Design Patterns
1. Creational Design Patterns
Creational patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.
- TypeScript Singleton Pattern Example
- TypeScript Factory Design Pattern with Example
- TypeScript Abstract Factory Pattern Example
- TypeScript Builder Pattern Example
- TypeScript Prototype Pattern Example
2. Structural Design Patterns
Structural patterns explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient.
- TypeScript Bridge Pattern Example
- TypeScript Adapter Pattern Example
- TypeScript Decorator Pattern Example
- TypeScript Composite Pattern Example
- TypeScript Flyweight Design Pattern Example
- TypeScript Facade Pattern Example
- TypeScript Proxy Pattern Example
3. Behavioral Design Patterns
Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.
- TypeScript Command Pattern Example
- TypeScript Chain of Responsibility Pattern Example
- TypeScript Visitor Pattern Example
- TypeScript Template Method Pattern Example
- TypeScript Strategy Pattern Example
- TypeScript State Pattern Example
- TypeScript Observer Pattern Example
- TypeScript Memento Pattern Example
- TypeScript Mediator Pattern Example
- TypeScript Iterator Pattern Example
- TypeScript Interpreter Design Pattern Example