In this article, we will learn how to use and implement the Facade Pattern in TypeScript with an example.
Facade is a structural design pattern that provides a simplified interface to a library, a framework, or any other complex set of classes.
TypeScript Facade Pattern Example
Let's create facade.ts file and add the following code to it:
export class Part1 {
public method1(): void {
console.log("`method1` of Part1");
}
}
export class Part2 {
public method2(): void {
console.log("`method2` of Part2");
}
}
export class Part3 {
public method3(): void {
console.log("`method3` of Part3");
}
}
export class Facade {
private part1: Part1 = new Part1();
private part2: Part2 = new Part2();
private part3: Part3 = new Part3();
public operation1(): void {
console.log("`operation1` is called ===");
this.part1.method1();
this.part2.method2();
console.log("==========================");
}
public operation2(): void {
console.log("`operation2` is called ===");
this.part1.method1();
this.part3.method3();
console.log("==========================");
}
}
Usage
Let's create demo.ts file and add the following code to it:
import { Facade } from "./facade";
export function show() : void {
var facade: Facade = new Facade();
facade.operation1();
facade.operation2();
}
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-master\facade> tsc --target ES5 .\demo.ts
design_patterns_in_typescript-master\facade> node .\demo.js
`operation1` is called ===
`method1` of Part1
`method2` of Part2
==========================
`operation2` is called ===
`method1` of Part1
`method3` of Part3
==========================
When to Use Facade Pattern
Use the Facade pattern when you need to have a limited but straightforward interface to a complex subsystem.
Use the Facade when you want to structure a subsystem into layers.
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