Danh mụcThẻBài viết

admin

I'm a Full-stack developer

Thẻ

Linked List
Data Structure
Chat GPT
Design Pattern
Microservices
API
AWS CDK
ReactJS
AWS Lightsail
Flutter Mobile
TypeScript Design Pattern - Adapter
Ngày đăng: 08/08/2023

What is an adapter pattern?

The Adapter Design Pattern is a structural design pattern. This design pattern acts as a bridge between two different interfaces. It can convert the interface of a class, to make it compatible with a client who is expecting a different interface, without changing the source code of the class.


When should I use the adapter pattern?

The adapter pattern should be used when:

  • Base code depends on 3rd party code that usually changes.
  • Code organization is efficient and easier to extend.


How to implement

Interface for each adapter:
interface IPayment {
  pay(): void;
}


Concrete implement of ApplePay & ApplePayAdapter class
export class ApplePay {
  checkout() {
    return `${this.startTransaction()} - ${this.transfer()} - ${this.completedTransaction()}`;
  }

  private startTransaction() {
    return 'Apple pay start transaction';
  }

  private transfer() {
    return 'Apple pay is transferring';
  }

  private completedTransaction() {
    return 'Apple pay transaction completed';
  }
}

export class ApplePayAdapter implements IPayment {
  private readonly _applePay: ApplePay;

  constructor() {
    this._applePay = new ApplePay();
  }
  
  pay() {
    return this._applePay.checkout();
  }
}


Concrete implement of StripePay & StripePayAdapter class
export class StripePay {
  private readonly _money: number;

  constructor(money: number) {
    this._money = money;
  }

  charge() {
    return `Stripe pay charge: ${this._money}$`;
  }
}

export class StripePayAdapter implements IPayment {
  private readonly _stripePay: StripePay;

  constructor(money: number) {
    this._stripePay = new StripePay(money);
  }
  
  pay() {
    return this._stripePay.charge();
  }
}


The client code:
function client() {
  const stripe = new StripePayAdapter(150);
  const apple = new ApplePayAdapter();

  console.log(stripe.pay());
  console.log(apple.pay());
}

client();


Result:
Stripe pay charge: 150$

Apple pay start transaction - Apple pay is transferring - Apple pay transaction completed


Pros and Cons

Pros:

  • Single Responsibility Principle. You can separate the interface or data conversion code from the primary business logic of the program.
  • Open/Closed Principle. You can introduce new types of adapters into the program without breaking the existing client code, as long as they work with the adapters through the client interface.


Cons:

  • The overall complexity of the code increases because you need to introduce a set of new interfaces and classes. Sometimes it’s simpler just to change the service class so that it matches the rest of your code.

Wrapping Up

Thank you for reading, and happy coding!

I hope this article will help make the concepts of the Adapter Pattern

Đề xuất

Create Project with Express + TypeScript + ESLint + Auto Reload
admin12/06/2023

Create Project with Express + TypeScript + ESLint + Auto Reload
In this article, I introduce to you how to initialize an Express + TypeScript project.
Design Patterns
admin07/08/2023

Design Patterns
The design pattern does not be a specific programming language. Almost programming languages might apply design patterns that to resolve a problem repeat.
TypeScript Design Pattern - Abstract Factory
admin07/08/2023

TypeScript Design Pattern - Abstract Factory
The abstract factory pattern is one of five design patterns in the Creational Design Pattern group. The abstract factory provides an interface for creating families of related or dependent objects without specifying their concrete classes.
Mới nhất

TypeScript Design Pattern - Adapter
admin08/08/2023

TypeScript Design Pattern - Adapter
This design pattern acts as a bridge between two different interfaces.
Microservice in a Monorepo
admin22/06/2023

Microservice in a Monorepo
Microservice in a Monorepo
How to create scroll animations with Next.js App
admin08/04/2024

How to create scroll animations with Next.js App
A Beginner's Guide to Using AOS Library with Next.js application
Đinh Thành Công Blog

My website, where I write blogs on a variety of topics and where I have some experiments with new technologies.

hotlinelinkedinskypezalofacebook
DMCA.com Protection Status
Góp ý
Họ & Tên
Số điện thoại
Email
Nội dung
Tải ứng dụng
hotline

copyright © 2023 - AGAPIFA

Privacy
Term
About