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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.kkjavatutorials.adapter.model; /** * Model class which represents electricity * power in volts * @author KK JavaTutorials */ public class Volt { private int volts; public Volt(int volts) { this.volts = volts; } public int getVolts() { return volts; } @Override public String toString() { return "Volt [volts=" + volts + "]"; } } |
WallSocket.java
1 2 3 4 5 6 7 8 9 10 |
package com.kkjavatutorials.adapter; import com.kkjavatutorials.adapter.model.Volt; /** * @author KK JavaTutorials * Generates Electric power in Volts */ public interface WallSocket { public abstract Volt getVolts(); } |
MobileAdapter.java
1 2 3 4 5 6 7 8 9 10 |
package com.kkjavatutorials.adapter; import com.kkjavatutorials.adapter.model.Volt; /** * @author KK JavaTutorials * MobileAdapter has to generate 3 volts of power. */ public interface MobileAdapter { public Volt get3Volt(); } |
WallSocketImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.kkjavatutorials.adapter.impl; import com.kkjavatutorials.adapter.WallSocket; import com.kkjavatutorials.adapter.model.Volt; /** * @author KK JavaTutorials *Implememtion of WallSocket, which by default *generates 240 volts of power. */ public class WallSocketImpl implements WallSocket { @Override public Volt getVolts() { return new Volt(240); } } |
MobileAdapterImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.kkjavatutorials.adapter.impl; import com.kkjavatutorials.adapter.MobileAdapter; import com.kkjavatutorials.adapter.WallSocket; import com.kkjavatutorials.adapter.model.Volt; /** * @author KK JavaTutorials * Here MobileAdapter & WallSocket two incompatible interfaces * talk to each other using Adapter Design Pattern */ public class MobileAdapterImpl implements MobileAdapter { private WallSocket wallSocket; public MobileAdapterImpl(WallSocket wallSocket) { this.wallSocket = wallSocket; } @Override public Volt get3Volt() { Volt v240 = wallSocket.getVolts(); int v3 = v240.getVolts()/80; return new Volt(v3); } } |
ClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.kkjavatutorials.client; import com.kkjavatutorials.adapter.MobileAdapter; import com.kkjavatutorials.adapter.WallSocket; import com.kkjavatutorials.adapter.impl.MobileAdapterImpl; import com.kkjavatutorials.adapter.impl.WallSocketImpl; import com.kkjavatutorials.adapter.model.Volt; /** * @author KK JavaTutorials * Client program which shows how * two incompatible interfaces(WallSocket & MobileAdapter) * talk to each other using Adapter Design Pattern */ public class ClientTest { public static void main(String[] args){ //Creating WallSocket Reference WallSocket wallSocket = new WallSocketImpl(); //Generating 240 volts by default Volt v240 = wallSocket.getVolts(); System.out.println(v240); //Creating MobileAdapter Reference MobileAdapter mobileAdapter = new MobileAdapterImpl(wallSocket); //Generating 3 volts using Adapter design pattern Volt v3 = mobileAdapter.get3Volt(); System.out.println(v3); } } |
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.