Adapter Design Pattern in Java

The Adapter design pattern in Java is a way to make two objects with different interfaces work together. Sometimes, we have objects that we want to use together, but their interfaces are not compatible. In such cases, we can use the Adapter pattern.

The adapter pattern acts as a middleman or wrapper between these two objects. It takes the calls made to one object and translates them into a format that the other object can understand. This way, the two objects can collaborate seamlessly, even though they have incompatible interfaces.

Adapter Design Pattern in Java

Advantages of Using the Adapter Design Pattern

  • Compatibility: The Adapter pattern allows objects with incompatible interfaces to work together. This can be useful when working with legacy code or third-party libraries.
  • Reusability: The Adapter pattern can make code more reusable. For example, if you have a class that implements a specific interface, you can use the Adapter pattern to create an adapter class that allows the class to be used with other interfaces.
  • Flexibility: The Adapter pattern can make code more flexible. For example, if we have a class that uses a specific concrete implementation of an interface, we can use the Adapter pattern to create an adapter class that allows the class to be used with different concrete implementations of the interface.

Real Life Example of Adapter Design Pattern

One of the great real-life example of adapter design pattern is mobile charger. Mobile battery needs 3 volts to charge but the normal socket produces either 120V (US) or 240V (India). So, the mobile charger works as an adapter between mobile charging socket and the wall socket.

We will try to implement multi-adapter using adapter design pattern in this section. So, first of all we will have two classes - Volt (to measure volts) and Socket (producing constant volts of 120V).

Volt.java

Now we want to build an adapter that can produce 3 volts, 12 volts and default 120 volts. In order to achieve this, we will create an adapter interface with these methods.

Two Way Adapter Pattern

While implementing Adapter pattern, there are two approaches class adapter and object adapter. However, both these approaches produce same result.

Class Adapter

The Class Adapter pattern uses Java inheritance to extend the source interface and provide adapter functionality. The adapter class inherits from the source interface and implements the target interface. It then overrides the methods of the source interface to delegate to the target interface.

Adapter Design Pattern in Java

Advantages:

  • Easy to implement.
  • More efficient, as there is no additional object overhead.

Disadvantages:

  • Less flexible, as the adapter class is tightly coupled to the source interface.
  • It can lead to multiple inheritance problems.

Object Adapter

The Object Adapter pattern uses Java composition to wrap the source object and provide adapter functionality. The adapter class contains an instance of the source object and implements the target interface. It then delegates call to the target interface to the source object.

Adapter Design Pattern in Java

Advantages:

  • More flexible, as the adapter class is not tightly coupled to the source interface.
  • It does not lead to multiple inheritance problems.

Disadvantages:

  • It is more complex to implement.
  • Less efficient, as there is additional object overhead.

Class Adapter Implementation

Here is the class adapter approach implementation for adapter.

SocketClassAdapterImpl.java

Object Adapter Implementation

Here is the Object adapter implementation for adapter.

SocketObjectAdapterImpl.java

Note that both the adapter implementations are almost same and they implement the SocketAdapter interface. The adapter interface can also be an abstract class. Here, is a test program to consume our adapter design pattern implementation.

AdapterPatternTest.java

Output:

v3 volts using Class Adapter=3
v12 volts using Class Adapter=12
v120 volts using Class Adapter=120
v3 volts using Object Adapter=3
v12 volts using Object Adapter=12
v120 volts using Object Adapter=120

Overall, the Adapter design pattern is a powerful tool that can be used to solve a variety of problems. It is especially useful when working with incompatible interfaces or legacy code.






Latest Courses