CategoryTagArticle

admin

I'm a Full-stack developer

Tag

Linked List
Data Structure
Chat GPT
Design Pattern
Microservices
API
AWS CDK
ReactJS
AWS Lightsail
Flutter Mobile
TypeScript Design Pattern - Adapter
Published date: 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

Recommend

TypeScript Design Pattern - Bridge
admin08/08/2023

TypeScript Design Pattern - Bridge
Decouple an abstraction from its implementation so that the two can vary independently.
TypeScript Design Pattern - Adapter
admin08/08/2023

TypeScript Design Pattern - Adapter
This design pattern acts as a bridge between two different interfaces.
Writing a Data Transformation Pipeline Using Go
admin20/03/2024

Writing a Data Transformation Pipeline Using Go
In this article, I will show how to implement data processing using the Go programing language with a simple tutorial.
Newest

TypeScript Design Pattern - Adapter
admin08/08/2023

TypeScript Design Pattern - Adapter
This design pattern acts as a bridge between two different interfaces.
Create Cognito User Pool with AWS CDK
admin09/06/2023

Create Cognito User Pool with AWS CDK
In the previous post, I showed you how to create a simple S3 bucket. Next, in this article, I will guide you to create a Cognito User Pool.
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
Feedback
Name
Phone number
Email
Content
Download app
hotline

copyright © 2023 - AGAPIFA

Privacy
Term
About