sota_333ad4b72095606ab40c

Sota

Posted on November 30, 2024

Adapter Pattern

What is Adapter Pattern?

Adapter pattern is a structural pattern that converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

When to use it?

  • Use Adapter pattern when you want to use legacy code or third-party libraries but its interface is incompatible with other part of your application.

Problem

We've been using an old printer but now new free software is invented which is integrated with new printer. The problem is we can't afford to buy new printer but still want to use the software. Is there a way of using new software but with old printer which is incompatible with the software? Yes, here Adapter pattern comes in handy.

Solution

Image description

  1. NewSoftware
    This is our Client. This only accepts ModernPrinter interface. NewSoftware thinks it's dealing with ModernPrinter.

  2. ModernPrinter
    Provides compatible interface for Client.

  3. PrinterAdapter
    Receives a method call modernPrint() from ModernPrinter, then translates it into a format the

  4. OldPrinter
    This is what we want to use but its interface is incompatible with our NewSoftware. oldPrint() gets executed when ModernPrinter calls modernPrint().

Structure

Image description

Remember an adapter can be used with any subclass of the adaptee.

Implementation in Java

public class OldPrinter {

    public void oldPrint(String document) {
        System.out.println(document + " by old printer");
    }
}
Enter fullscreen mode Exit fullscreen mode
public interface ModernPrinter {

    void modernPrint(String document);
}
Enter fullscreen mode Exit fullscreen mode
public class PrinterAdapter implements ModernPrinter {

    private OldPrinter oldPrinter;

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

    @Override
    public void modernPrint(String document) {
        oldPrinter.oldPrint(document);
    }
}
Enter fullscreen mode Exit fullscreen mode
public class NewSoftware {

    public static void main(String[] args) {
        OldPrinter oldPrinter = new OldPrinter();
        // Because PrinterAdapter implements ModernPrinter, it can be seen
        // as a ModernPrinter which is integrated with NewSoftware
        ModernPrinter adapter = new PrinterAdapter(oldPrinter);
        adapter.modernPrint("Hello world");
    }
}
Enter fullscreen mode Exit fullscreen mode

In our assumption, you NewSoftware is integrated with ModernPrinter, so it doesn't make sense to code like oldPrinter.oldPrint("Hello world"); in NewSoftware class (even though it will be compiled without errors).

Output:

Hello world by old printer
Enter fullscreen mode Exit fullscreen mode

Pitfalls

  • An adapter that does a lot of work besides simple interface translation results in adding/altering adaptee behavior, which is far from the intent of Adapter pattern.

Object and Class Adapter

What we’ve seen is called an Object Adapter, which adapts the adaptee using composition. There is another type of adapter called a Class Adapter, which uses inheritance to achieve the intent of the Adapter pattern.

Java doesn't support multiple inheritance. So we can't use class adapter in Java, but we will study it anyway as you might encounter class adapter in other language.

Image description

Because object adapter uses composition, Adapter can hold references to one or more Adaptee objects. Thus, object adapter offers more flexibility. While class adapter is tightly coupled with one and only one adaptee which leads to less flexibility.


You can check all the design pattern implementations here.
GitHub Repository

💖 💪 🙅 🚩
sota_333ad4b72095606ab40c
Sota

Posted on November 30, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Adapter Pattern
computerscience Adapter Pattern

November 30, 2024

Singleton Pattern
computerscience Singleton Pattern

November 22, 2024

Simple Factory
computerscience Simple Factory

November 10, 2024