Skip to main content

Command Palette

Search for a command to run...

Adapter Design Pattern

Updated
5 min read
Adapter Design Pattern

Introduction

Hello all, today I back with another Design Patterns topic, and today I will share what i learned about Adapter design pattern. I hope with this article you will know more about Adapter design pattern and use it on your project.

What It Is?

The Adapter design pattern is a structural pattern that allows objects with incompatible interfaces to work together. It acts as a bridge between incompatible interfaces by making one interface compatible with another.

When To Use It?

Here are some scenarios where implementing the Adapter design pattern can be beneficial to a project:

  • When two or more classes or systems with incompatible interfaces need work together.

  • When there’s a need to integrate new components with legacy code that uses different or outdated interfaces.

  • When there’s a need to use a third-party library or external service that doesn't match the design of the current system.

  • When the existing code can’t be modified but still needs to work with the new system.

Why Implement It?

Here are some pros and cons to consider before implementing the Adapter design pattern in a project:

Pros

  • Increases compatibility by allowing systems with incompatible interfaces to work together without modifying their source code.

  • Adheres to the Single Responsibility Principle by isolating interface-related logic within the adapter classes.

  • Adheres to the Open-Closed Principle by enabling the creation of new adapters without modifying the existing system's code.

  • Increases scalability by allowing seamless integration of new components or systems without modifying existing code.

Cons

  • Increases complexity by adding an adapter layer to the system, which can make debugging and maintaining the code more difficult.

  • May require constant maintenance if the interface of the adapted class changes regularly.

How To Implement It?

Here are the steps to implement the Adapter design pattern in a project:

  1. Identify the existing class (Adaptee) that has an incompatible interface with the current system.

  2. Create an interface or abstract class that has a compatible interface with the current system.

  3. Create an adapter class that implements the interface or abstract class.

  4. Create a field in the adapter class to store a reference to the existing class (Adaptee) so the adapter can access the existing class's incompatible interface.

  5. Implement the methods defined in the interface or abstract class in the adapter class. Within these methods, call the corresponding methods (The incompatible interface) of the existing class (Adaptee) to make it compatible with the current system.

  6. Use the adapter class in the client to interact with the existing class (Adaptee) seamlessly.

Structure

Pseudocode

BEGIN CLASS Adaptee
    BEGIN PUBLIC FUNCTION getSpecificRequest()
        RETURN "Specific request."
    END FUNCTION
END CLASS
BEGIN INTERFACE IAdapter
    FUNCTION getRequest(): string
END INTERFACE
BEGIN CLASS Adapter IMPLEMENTS IAdapter
    PRIVATE FIELD adaptee

    BEGIN PUBLIC CONSTRUCTOR Adapter(adaptee)
        this.adaptee = adaptee
    END CONSTRUCTOR

    BEGIN PUBLIC FUNCTION getRequest()
        return adaptee.getSpecificRequest()
    END FUNCTION
END CLASS
BEGIN CLASS Client
    BEGIN PRIVATE PROCEDURE useAdapter()
        FIELD adaptee = new Adaptee()
        FIELD iAdapter = new Adapter(adaptee)
        FIELD result = iAdapter.getRequest()
    END PROCEDURE
END CLASS
The source code for the Adapter design pattern in various programming languages can be viewed and downloaded on the Project Repository – Github.

Additional Information

I have discovered some additional information about Adapter design pattern. If you have any additional information about Adapter design pattern that you'd like to share, please feel free to leave a comment.

Adapter Design Pattern VS Facade Design Pattern

Both Adapter and Facade design patterns have similar code structures. Due to these similarities, choosing between them can be challenging. Here are some key factors to consider before making a decision:

  • The Adapter design pattern focuses on making two incompatible interfaces work together by providing a translation layer between them, while the Facade design pattern focuses on simplifying the interaction with complex subsystems by providing a unified interface.

  • The Adapter design pattern usually works with a single class or component, whereas the Facade design pattern works with a set of classes or an entire subsystem.

Adapter Design Pattern VS Decorator Design Pattern

Both Adapter and Decorator design patterns have similar code structures. Due to these similarities, choosing between them can be challenging. Here are some key factors to consider before making a decision:

  • The Adapter design pattern focuses on making two incompatible interfaces work together by providing a translation layer between them, while the Decorator design pattern focuses on adding new functionality to objects.

  • The Adapter design pattern usually works with a single class or component (One adapter per one incompatible interface), whereas the Decorator design pattern often involves stacking multiple decorators to add several layers of functionality.

Further Discoveries

Here are some further discoveries I have gathered from various sources that may provide a deeper understanding of Adapter design pattern:

  • The Adapter design pattern can be implemented using either inheritance (extending the Adaptee class) or composition (using an object reference inside the adapter). Composition is generally preferred because it avoids tightly coupling between the adapter and the Adaptee class.

  • The Adapter design pattern doesn't have to implement all methods from the target interface, instead it only needs to "adapt" the necessary methods of the Adaptee class to ensure compatibility with the current system.

  • The Adapter design pattern can be combined with the Facade design pattern to provide a simpler interface for the client to interact with multiple adaptees.

  • The Adapter design pattern can be combined with the Decorator design pattern to add extra functionality to an adaptee without changing its core structure.

Reference

  1. Adapter Design Pattern – Sourcemaking

  2. Adapter Design Pattern – Refactoring.guru

Design Patterns

Part 2 of 4

In this series, I’ll share insights about design patterns. From foundational concepts to advanced applications, discover how these patterns enhance code organization, improve efficiency, and elevate your software development projects.

Up next

Builder Design Pattern

Introduction Hello all, today I back with another Design Patterns topic, and today I will share what i learned about Builder design pattern. I hope with this article you will know more about Builder design pattern and use it on your project. What It ...