Adapter pattern comes under Structural Design Pattern. This pattern combines the capability of two independent interfaces and works as a bridge between two incompatible interfaces.
The Adapter pattern usually involves one single class which provides functionality to reuse the incompatible interface. The bridge class is adapted as per client requirement and provides the functions of the incompatible interface. You take this approach if:
- You need to reuse the class that does not have an interface as per client requirement
- Your intent is to provide an alternative interface for a class
xHere are ALL other Java Design Patterns, explained in detail with examplesx
Adapter Method Pattern by Example
This pattern can be understood with a simple real-life example of charger and socket adapter. When you travel across the country, you will find the electrical plugs are different and you can not directly use the plug points for your devices.
So what do you do in such scenario? you use an Adapter which has the sockets that are compatible with your device and it also has country-specific extensions which you can choose based on your country of travel. Well, this is what exactly achieved in Adapter Pattern.
Let us try to convert the above example into code. At first, define a simple adapter interface with the connect method.
public interface Adapter { public void conntect(); }
Now we will have a couple of concrete implementation of the above adapter
for the UK
public class UKAdapter implements Adapter { @Override public void conntect() { System.out.println("Connecting to UK Plug using Adapter."); } }
for Germany
public class GermanyAdapter implements Adapter { @Override public void conntect() { System.out.println("Connecting to Germany Plug using Adapter."); } }
Below is our device charger class
public class Charger { private String country; private Adapter adapter; public void charge() { adapter.conntect(); System.out.println("Charging..."); } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public void setAdapter(Adapter adapter) { this.adapter = adapter; } }
Observer that we have an instance of Adapter into our charger. This adapter will be used to connect to the country-specific socket.
Running the Example
public class AdapterDemo { public static void main(String[] args) { Charger charger = new Charger(); charger.setAdapter(new UKAdapter()); charger.charge(); charger.setAdapter(new GermanyAdapter()); charger.charge(); } }
Output:
Connecting to UK Plug using Adapter. Charging... Connecting to Germany Plug using Adapter. Charging...
Advantages:
- The main advantage of this pattern is that it helps achieve reusability and flexibility.
- A client can use a different interface and can use polymorphism to use different implementations of adapters.
Conclusion
In this article, we understood the Adapter Pattern with the help of a simple example. The source code is available in our Github repository.
Download Codex
x
Similar to Flyweight Pattern, if you wish to have a look at other Java Design Patterns, then check this out. You can also find more sample in another language at Wiki Page