BCA / B.Tech 9 min read

Adapter Pattern in Java

Adapter Pattern in Java:


What is the Adapter Pattern?

  • The Adapter Pattern is a Structural Design Pattern used when we need to make an old class work with a new interface.
  • It acts like a "translator," helping to connect two different interfaces.
Why use the Adapter Pattern?

  • Suppose you have an old class that was already working in another system, and now you need to connect it to a new system, but their interfaces are different. If you directly change the old class, a lot of code will have to be rewritten, which would be a waste of time and resources.
Problem:

  • The interfaces of the old class and the new system are different.
  • The old class needs to be integrated into the new system without being modified.
Solution:

  • Use the Adapter Pattern, which will act as a bridge between the two.
Real-life Example of Adapter Pattern

Multi-plug Adapter:

  • Suppose you have a charger brought from America, which has a 2-pin plug.
  • But in India, there are 3-pin sockets.
  • Now, you either change the charger (which would be expensive) or use an adapter (converter) that converts the 2-pin to a 3-pin.
  • The Adapter Pattern does the same thing – it acts as a bridge between the old system and the new system.
Code Example of Adapter Pattern (in Java)
Now let's understand it through code.

Old Interface
// The old class that is already working
class OldCharger {
    void chargeWithTwoPins() {
        System.out.println("Charging with 2 pins...");
    }
}
New Interface
// The new interface that our system needs to support
interface NewCharger {
    void chargeWithThreePins();
}
Adapter Class
// The adapter that converts the old class (OldCharger) to the new interface (NewCharger)
class ChargerAdapter implements NewCharger {
    private OldCharger oldCharger;

    // The old class is passed in the constructor
    public ChargerAdapter(OldCharger oldCharger) {
        this.oldCharger = oldCharger;
    }

    @Override
    public void chargeWithThreePins() {
        System.out.println("Adapter is being used...");
        oldCharger.chargeWithTwoPins(); // The old charger is being used
    }
}
Client Code
public class AdapterPatternExample {
    public static void main(String[] args) {
        // Old charger object created
        OldCharger oldCharger = new OldCharger();

        // Using the old charger with the new system via the adapter
        NewCharger adapter = new ChargerAdapter(oldCharger);

        // The new system demands a three-pin charger, but we make it possible with the adapter
        adapter.chargeWithThreePins();
    }
}
Output:
  • Adapter is being used...
  • Charging with 2 pins...
Advantages of the Adapter Pattern:

  • Can use old code in a new system without changing it.
  • Increases code reusability.
  • Can easily integrate new and old technologies.
  • Increases flexibility in the system.

Disadvantages of the Adapter Pattern:

  • Can become a bit complex if there are many interfaces.
  • May have a slight impact on performance as an extra layer is added.