Thilan Dissanayaka Design Patterns Apr 26

Adapter Pattern explained simply

Ever needed to connect two incompatible interfaces without changing their source code?
That’s exactly where the Adapter Pattern shines!

The Adapter Pattern is a structural design pattern that allows objects with incompatible interfaces to work together. It acts like a bridge between two different interfaces.

What is the Adapter Pattern?

At its core, the Adapter Pattern:

  • Converts the interface of a class into another interface the client expects.
  • Allows classes to work together that otherwise couldn't due to incompatible interfaces.
  • Promotes code reusability and flexibility.

In simple words:

It’s like a translator who helps two people speaking different languages communicate!

A Real-Life Analogy

Imagine you have a laptop charger with a US plug, but you're traveling in Europe where sockets are different.
You don't throw away your charger — you use a power adapter!

Similarly, in software, instead of rewriting code, we adapt it.

A Quick Example (Without Adapter)

Suppose you have an OldPrinter class:

public class OldPrinter {
    public void printDocument(String text) {
        System.out.println("Printing document: " + text);
    }
}

Now you build a new system that expects every printer to have a print() method instead:

public interface Printer {
    void print(String text);
}

Problem: OldPrinter doesn't match the new Printer interface.
You can’t directly use OldPrinter in your new system.

Enter the Adapter Pattern

We create an Adapter class that implements the Printer interface and uses an instance of OldPrinter internally.

public class PrinterAdapter implements Printer {
    private OldPrinter oldPrinter;

    public PrinterAdapter(OldPrinter oldPrinter) {
        this.oldPrinter = oldPrinter;
    }

    @Override
    public void print(String text) {
        oldPrinter.printDocument(text);
    }
}

Now, you can use OldPrinter seamlessly in the new system!

public class MainProgram {
    public static void main(String[] args) {
        OldPrinter oldPrinter = new OldPrinter();
        Printer printer = new PrinterAdapter(oldPrinter);

        printer.print("Hello, World!");
    }
}

Output:

Printing document: Hello, World!

Key Components

  • Target Interface (Printer): The interface your client code expects.
  • Adaptee (OldPrinter): The existing class that needs adapting.
  • Adapter (PrinterAdapter): Bridges the gap between the Target and Adaptee.

Types of Adapter Pattern

There are mainly two ways to implement Adapter Pattern:

1. Class Adapter (using Inheritance)

  • Adapter extends Adaptee and implements the Target interface.
  • Not very flexible because Java supports single inheritance only.

2. Object Adapter (using Composition) ⭐

  • Adapter has an instance of Adaptee.
  • More flexible and preferred in most cases.

Note: The example above is an Object Adapter.

When to Use the Adapter Pattern?

  • When you want to use an existing class but its interface doesn't match your needs.
  • When you want to create a reusable class that cooperates with unrelated classes.
  • When you need to work with legacy code without modifying it.

Real World Use Cases

  • Legacy system integration: Adapting old APIs to work with modern ones.
  • Third-party library integration: Adapting library classes to your own interfaces.
  • UI component libraries: Adapting different UI components under a single standard.

Advantages

✅ Promotes code reuse.
✅ Makes incompatible classes work together.
✅ Follows the Open/Closed Principle — open for extension but closed for modification.

Disadvantages

❌ Increases code complexity due to additional classes.
❌ Overuse can lead to too many adapters, making the code harder to maintain.

Final Thoughts

The Adapter Pattern is like a universal connector in software development.
It allows your systems to evolve without needing risky and expensive rewrites.

Whenever you encounter a mismatch between interfaces, think:

"Can I just adapt it instead of rewriting it?"

Happy coding! 🚀

ALSO READ
Decorator Pattern explained simply
Apr 26 Design Patterns

When you want to **add new functionalities** to an object **without modifying its structure**, the **Decorator Pattern** comes to the rescue. The Decorator Pattern lets you dynamically **wrap**....

Proxy Pattern explained simply
Apr 26 Design Patterns

Sometimes you don't want or can't allow direct access to an object. Maybe it's expensive to create, needs special permissions, or you want to control access in some way. This is where the **Proxy....

Remote Command Execution
Mar 23 Web App Hacking

Remote Command Execution (RCE) is a critical security vulnerability that allows an attacker to execute arbitrary commands on a remote server. This vulnerability can lead to unauthorized access, data....

Introduction to Edge Computing
Mar 23 Software Architecture

Edge computing is a distributed computing paradigm where computation and data storage are performed closer to the location where it is needed. Instead of relying solely on a centralized data center,....

Template Pattern explained simply
Apr 26 Design Patterns

Ever found yourself writing similar logic over and over, only to change a few steps each time? That’s exactly what the **Template Pattern** helps you solve. The **Template Pattern** is a....

Tic-Tac-Toe Game with Atmega 256 MicroController
Mar 23 Software Architecture

In this blog, I’ll walk you through how I built a **Tic-Tac-Toe game** using an AVR microcontroller, a 4x3 keypad, and a grid of LEDs. This project combines the basics of embedded programming, game....