Adapter Design Pattern in Java

By | July 4, 2020

In this post, We will talk and learn about the Adapter Design Pattern in Java.

  • The Adapter design pattern falls one under structural design pattern
  • It is used so that two unrelated interfaces can work together.
  • The object that joins these unrelated interfaces is called an Adapter.

Adapter Design Pattern Example in JDK :

  • java.io.InputStreamReader(InputStream)
  • java.io.OutputStreamWriter(OutputStream)
  • javax.xml.bind.annotation.adapters.XmlAdapter#marshal() and #unmarshal()

Below is the complete source code:

Adapter Design Pattern Project

Now Let’s move towards the implementation of the Adapter Design Pattern.

Here I have taken a Scenario Where WallSocket generates 240 volts electricity by default but MobileAdapter using the Adapter Design Pattern able to generate 3 volts of electricity.

Idea is that How these two incompatible interfaces(WallSocket & MobileAdapter)  talk to each other using Adapter Design Pattern

Below is the complete source code:

Volt.java

 

WallSocket.java 

MobileAdapter.java

WallSocketImpl.java 

MobileAdapterImpl.java

ClientTest.java

Output of this client program:

Volt [volts=240]
Volt [volts=3]

You May Also Like:

Singleton Design Pattern in java
Prototype Pattern in Java
Factory Pattern in Java
Abstract Factory Pattern in Java
Builder Pattern in Java

That’s all about the Adapter Design Pattern in Java.
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *