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 S3 Bucket with AWS CDK
admin09/06/2023

Create S3 Bucket with AWS CDK
In this article, I introduce Amazon CDK and how to write AWS infrastructure-as-code using TypeScript. We will do it step by step.
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.
TypeScript Design Pattern - Builder
admin07/08/2023

TypeScript Design Pattern - Builder
TypeScript Design Pattern - Builder
Mới nhất

NodeJS Verify and Decode Cognito JWT Tokens
admin12/06/2023

NodeJS Verify and Decode Cognito JWT Tokens
In this article, I will show you how to verify and decode the Cognito JWT Tokens token.
Data structure: Singly Linked List
admin07/04/2024

Data structure: Singly Linked List
In this article, I will show you how to set up a singly linked list algorithm in Python.
Part 2: The hooks are used popularly in React
admin18/06/2023

Part 2: The hooks are used popularly in React
As a newbie React developer, does not understand when is use stateless (functional) components or stateful components. React hook is a new feature from v16.8, the developer does not worry about react lifecycle, and it is difficult to learn for newbies.
Đ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