TypeScript Adapter Pattern Example

In this article, we will learn how to use and implement the Adapter Pattern in TypeScript with an example.
Adapter is a structural design pattern that allows objects with incompatible interfaces to collaborate.

Adapter design pattern is one of the structural design patterns and it's used so that two unrelated interfaces can work together. The object that joins this unrelated interface is called an Adapter.

TypeScript Abstract Factory Pattern Example

Let's create abstractFactory.ts file and add the following code to it:
export class Adaptee {
    public method(): void {
        console.log("`method` of Adaptee is being called");
    }
}

export interface Target {
    call(): void;
}

export class Adapter implements Target {
    public call(): void {
        console.log("Adapter's `call` method is being called");
        var adaptee: Adaptee = new Adaptee();
        adaptee.method();
    }
}

Usage

Let's create demo.ts file and add the following code to it:
import { Adapter } from "./adapter";

export function show() : void {
 var adapter: Adapter = new Adapter();
 adapter.call();
}
show();
Run:
  • Compile the above code using the TypeScript compiler.
  • Above code is compiled to plan JavaScript code
  • Run Javascript code using node
PS C:\Users\RAMESH\design_patterns_in_typescript\adapter> tsc --target ES5 .\demo.ts
PS C:\Users\RAMESH\design_patterns_in_typescript\adapter> node .\demo.js
Adapter's `call` method is being call

Check out how to install TypeScript compiler and run TypeScript file in VS Code at https://www.javaguides.net/2019/09/install-typescript-compiler-and-run-code.html

All TypeScript Design Patterns

1. Creational Design Patterns

Creational patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.
  1. TypeScript Singleton Pattern Example
  2. TypeScript Factory Design Pattern with Example
  3. TypeScript Abstract Factory Pattern Example
  4. TypeScript Builder Pattern Example
  5. 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.
  1. TypeScript Bridge Pattern Example
  2. TypeScript Adapter Pattern Example
  3. TypeScript Decorator Pattern Example
  4. TypeScript Composite Pattern Example
  5. TypeScript Flyweight Design Pattern Example
  6. TypeScript Facade Pattern Example
  7. TypeScript Proxy Pattern Example

3. Behavioral Design Patterns

Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.
  1. TypeScript Command Pattern Example
  2. TypeScript Chain of Responsibility Pattern Example
  3. TypeScript Visitor Pattern Example
  4. TypeScript Template Method Pattern Example
  5. TypeScript Strategy Pattern Example
  6. TypeScript State Pattern Example
  7. TypeScript Observer Pattern Example
  8. TypeScript Memento Pattern Example
  9. TypeScript Mediator Pattern Example
  10. TypeScript Iterator Pattern Example
  11. TypeScript Interpreter Design Pattern Example