codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎭

Design Patterns

12 / 100 topics
11Introduction to Structural Patterns12Adapter Pattern13Bridge Pattern14Composite Pattern15Decorator Pattern16Facade Pattern17Flyweight Pattern18Proxy Pattern32Practical Exercises for Structural Patterns
Tutorials/Design Patterns/Adapter Pattern
🎭Design Patterns

Adapter Pattern

Updated 2026-05-15
10 min read

Adapter Pattern

Introduction

In software design, the Adapter Pattern is one of the Structural Patterns that allows incompatible interfaces to work together by converting the interface of a class into another interface clients expect. This pattern is particularly useful when you need to integrate existing classes with new ones without modifying their source code.

The Adapter Pattern involves creating an adapter class that acts as a mediator between two incompatible interfaces, enabling them to collaborate seamlessly. This pattern promotes loose coupling and enhances reusability by allowing objects to interact through a common interface.

Concept

The core idea behind the Adapter Pattern is to create a wrapper or intermediary class that translates calls from one interface into another. This adapter class acts as a bridge between two incompatible interfaces, ensuring that the client code can work with the target object without needing to know its underlying implementation.

Key Components

  1. Target Interface: The interface expected by the client.
  2. Adaptee Class: The existing class whose interface needs to be adapted.
  3. Adapter Class: The intermediary class that implements the Target Interface and adapts calls to the Adaptee Class.

Examples

Let's explore a practical example to understand how the Adapter Pattern works.

Example 1: Audio Player

Suppose you have an audio player application that supports only MP3 files. However, you want to extend its functionality to support WAV files as well. The existing MP3Player class has a method playMp3, but there is no corresponding method for WAV files. We can use the Adapter Pattern to create a new interface that supports both MP3 and WAV files.

Step 1: Define the Target Interface

First, let's define the target interface that our adapter will implement:

JavaScript
1interface MediaPlayer {
2play(file: string): void;
3}

Step 2: Create the Adaptee Class

Next, we have an existing class MP3Player that can only play MP3 files:

JavaScript
1class MP3Player {
2playMp3(mp3File: string) {
3 console.log(`Playing MP3 file: ${mp3File}`);
4}
5}

Step 3: Implement the Adapter Class

Now, let's create an adapter class MP3ToWAVAdapter that implements the MediaPlayer interface and adapts calls to the MP3Player class:

JavaScript
1class MP3ToWAVAdapter implements MediaPlayer {
2private mp3Player: MP3Player;
3
4constructor(mp3Player: MP3Player) {
5 this.mp3Player = mp3Player;
6}
7
8play(file: string) {
9 if (file.endsWith('.mp3')) {
10 this.mp3Player.playMp3(file);
11 } else if (file.endsWith('.wav')) {
12 console.log(`Playing WAV file: ${file}`);
13 } else {
14 throw new Error('Unsupported file format');
15 }
16}
17}

Step 4: Use the Adapter

Finally, we can use the adapter to play both MP3 and WAV files:

JavaScript
1const mp3Player = new MP3Player();
2const mediaPlayer: MediaPlayer = new MP3ToWAVAdapter(mp3Player);
3
4mediaPlayer.play('song.mp3'); // Output: Playing MP3 file: song.mp3
5mediaPlayer.play('sound.wav'); // Output: Playing WAV file: sound.wav

Example 2: Payment Gateway Integration

Another common use case for the Adapter Pattern is integrating different payment gateways. Suppose you have a PayPal class that provides a method payWithPayPal, but your application needs to support both PayPal and Stripe payments.

Step 1: Define the Target Interface

First, let's define the target interface:

JavaScript
1interface PaymentGateway {
2pay(amount: number): void;
3}

Step 2: Create the Adaptee Class

Next, we have an existing class PayPal that can only process payments through PayPal:

JavaScript
1class PayPal {
2payWithPayPal(amount: number) {
3 console.log(`Paid ${amount} using PayPal`);
4}
5}

Step 3: Implement the Adapter Class

Now, let's create an adapter class PayPalToStripeAdapter that implements the PaymentGateway interface and adapts calls to the PayPal class:

JavaScript
1class PayPalToStripeAdapter implements PaymentGateway {
2private payPal: PayPal;
3
4constructor(payPal: PayPal) {
5 this.payPal = payPal;
6}
7
8pay(amount: number) {
9 // Simulate payment processing through Stripe
10 console.log(`Paid ${amount} using Stripe`);
11}
12}

Step 4: Use the Adapter

Finally, we can use the adapter to process payments through both PayPal and Stripe:

JavaScript
1const payPal = new PayPal();
2const paymentGateway: PaymentGateway = new PayPalToStripeAdapter(payPal);
3
4paymentGateway.pay(100); // Output: Paid 100 using Stripe

What's Next?

In the next section, we will explore another Structural Pattern called the Bridge Pattern. The Bridge Pattern is used to separate an abstraction from its implementation so that both can be varied independently.

Stay tuned for more insights into design patterns and how they can help you build robust and maintainable software systems!


PreviousIntroduction to Structural PatternsNext Bridge Pattern

Recommended Gear

Introduction to Structural PatternsBridge Pattern