Member-only story

The Adapter Pattern in TypeScript

A real-world example

Lewis Fairweather
ITNEXT
3 min readSep 3, 2024

--

The Adapter Pattern is a structural design pattern that allows incompatible interfaces to work together by providing a "wrapper" or "adapter" around an existing class. Below is a real-world example of the Adapter Pattern in TypeScript, where we want to integrate a new payment gateway into an existing e-commerce application.

Scenario:

Let’s say you are building an e-commerce application that uses an existing PayPalPaymentProcessor to process payments. You’re looking to support other payment providers such as Stripe so there’s a requirement to implement a new payment gateway. Let’s call this StripePaymentProcessor, however, its interface is different from the existing PayPalPaymentProcessor. To avoid changing the entire application to accommodate the new payment gateway, you can use the Adapter Pattern.

Step 1: Define the Target Interface

The PaymentProcessor interface is the target interface that the application understands.

interface PaymentProcessor {
pay(amount: number): void;
}

Step 2: Existing PayPal Payment Processor

This is the existing payment processor that the application already uses. As you can see…

--

--

Published in ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.

Responses (2)

Write a response