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: Build a Chat App with ReactJS + Material UI
admin13/09/2023

Part 1: Build a Chat App with ReactJS + Material UI
In this article, I would like to introduce using ReactJS and material UI to build a Chat App.
How to secure your API gateway
admin17/04/2024

How to secure your API gateway
In this blog, I will cover the 6 methods that technology leaders need to incorporate to secure and protect APIs.
TypeScript Design Pattern - Singleton
admin07/08/2023

TypeScript Design Pattern - Singleton
The singleton ensures only a single install is created in a class and provides a method to access this instance everywhere in a codebase.
Mới nhất

What are the SOLID principles?
admin17/06/2023

What are the SOLID principles?
If we want to have good software, the infrastructure should be good first. We should learn more techniques that help to have better quality.
TypeScript Design Pattern - Proxy
admin11/08/2023

TypeScript Design Pattern - Proxy
Provide a surrogate or placeholder for another object to control access to it.
How to secure your API gateway
admin17/04/2024

How to secure your API gateway
In this blog, I will cover the 6 methods that technology leaders need to incorporate to secure and protect APIs.
Đ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