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

Part 1: How to deploy Ghost Blog on AWS using Lightsail
admin17/06/2023

Part 1: How to deploy Ghost Blog on AWS using Lightsail
In this article, I want to introduce about how to deploy Ghost Blog on AWS using Lightsail.
🚀 Using Bitwise Oprators to build a RBAC in Node.js 🚀
admin13/04/2024

🚀 Using Bitwise Oprators to build a RBAC in Node.js 🚀
In this article, I will illustrate to you how to build an RBAC in Node.js using Bitwise Operators.
Form validator in Flutter with EzValidator
admin04/01/2024

Form validator in Flutter with EzValidator
When I am working on Flutter with form. For ensuring data integrity, and handling user input errors. I want to show an error message below each TextField, Dropdown, Switch, ... if the user does not input or wrong input. The EzValidator help me to resolve this.
Mới nhất

Semantic Versioning NodeJS
admin07/07/2023

Semantic Versioning NodeJS
How to Use Semantic Versioning in NPM
Part 4: Creating Static Home Page on Ghost CMS
admin17/06/2023

Part 4: Creating Static Home Page on Ghost CMS
I believe that many of you are asking the question: How to fix the home page of Ghost CMS as desired? and are struggling to find many different sources of documentation.
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.
Đ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