You are on page 1of 5

dapter design pattern is one of the structural design pattern and its used so that two unrelated

interfaces can work together. The object that joins these unrelated interface is called an Adapter.
As a real life example, we can think of a mobile charger as an adapter because 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 tutorial.
So first of all we will have two classes Volt (to measure volts) and Socket (producing
constant volts of 120V).
Volt.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

package com.journaldev.design.adapter;
public class Volt {
private int volts;
public Volt(int v){
this.volts=v;
}
public int getVolts() {
return volts;
}
public void setVolts(int volts) {
this.volts = volts;
}
}

Socket.java
1
2
3
4
5
6
7
8

package com.journaldev.design.adapter;
public class Socket {
public Volt getVolt(){
return new Volt(120);
}
}

Now we want to build an adapter that can produce 3 volts, 12 volts and default 120 volts. So first
of all we will create an adapter interface with these methods.
SocketAdapter.java
1

package com.journaldev.design.adapter;

2
3 public interface SocketAdapter {
4
5
public Volt get120Volt();
6
public Volt get12Volt();
7
8
public Volt get3Volt();
9
}
10

Two Way Adapter Pattern


While implementing Adapter pattern, there are two approaches class adapter and object
adapter, however both these approaches produce same result.
1. Class Adapter This form uses java inheritance and extends the source interface, in

our case Socket class.


2. Object Adapter This form uses Java Composition and adapter contains the source
object.

Class Adapter Implementation


Here is the class adapter approach implementation of our adapter.
SocketClassAdapterImpl.java
package com.journaldev.design.adapter;

1
2
3 //Using inheritance for adapter pattern
public class SocketClassAdapterImpl extends Socket implements SocketAdapter{
4
5
@Override
6
public Volt get120Volt() {
return getVolt();
7
}
8
9
@Override
10
public Volt get12Volt() {
11
Volt v= getVolt();
12
return convertVolt(v,10);
}
13
14
@Override
15
public Volt get3Volt() {
16
Volt v= getVolt();
17
return convertVolt(v,40);
}
18
19
private Volt convertVolt(Volt v, int i) {
20
return new Volt(v.getVolts()/i);
21
}

22
23
24
25}
26
27

Object Adapter Implementation


Here is the Object adapter implementation of our adapter.
SocketObjectAdapterImpl.java
1
2
3 package com.journaldev.design.adapter;
4
public class SocketObjectAdapterImpl implements SocketAdapter{
5
6
//Using Composition for adapter pattern
7
private Socket sock = new Socket();
8
9
@Override
public Volt get120Volt() {
10
return sock.getVolt();
11
}
12
13
@Override
14
public Volt get12Volt() {
Volt v= sock.getVolt();
15
return convertVolt(v,10);
16
}
17
18
@Override
19
public Volt get3Volt() {
20
Volt v= sock.getVolt();
return convertVolt(v,40);
21
}
22
23
private Volt convertVolt(Volt v, int i) {
24
return new Volt(v.getVolts()/i);
25
}
26}
27
28
Notice 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 implementation.

AdapterPatternTest.java
1
2
3 package com.journaldev.design.test;
4
5 import com.journaldev.design.adapter.SocketAdapter;
import com.journaldev.design.adapter.SocketClassAdapterImpl;
6 import com.journaldev.design.adapter.SocketObjectAdapterImpl;
7 import com.journaldev.design.adapter.Volt;
8
9 public class AdapterPatternTest {
10
public static void main(String[] args) {
11
12
testClassAdapter();
13
testObjectAdapter();
14
}
15
16
private static void testObjectAdapter() {
SocketAdapter sockAdapter = new SocketObjectAdapterImpl();
17
Volt v3 = getVolt(sockAdapter,3);
18
Volt v12 = getVolt(sockAdapter,12);
19
Volt v120 = getVolt(sockAdapter,120);
20
System.out.println("v3 volts using Object Adapter="+v3.getVolts());
21
System.out.println("v12 volts using Object
22Adapter="+v12.getVolts());
System.out.println("v120 volts using Object
23
Adapter="+v120.getVolts());
24
}
25
26
private static void testClassAdapter() {
SocketAdapter sockAdapter = new SocketClassAdapterImpl();
27
Volt v3 = getVolt(sockAdapter,3);
28
Volt v12 = getVolt(sockAdapter,12);
29
Volt v120 = getVolt(sockAdapter,120);
30
System.out.println("v3 volts using Class Adapter="+v3.getVolts());
31
System.out.println("v12 volts using Class Adapter="+v12.getVolts());
System.out.println("v120 volts using Class
32
Adapter="+v120.getVolts());
33
}
34
35
private static Volt getVolt(SocketAdapter sockAdapter, int i) {
36
switch (i){
37
case 3: return sockAdapter.get3Volt();
case 12: return sockAdapter.get12Volt();
38
case 120: return sockAdapter.get120Volt();
39
default: return sockAdapter.get120Volt();
40
}
41
}
42}
43
44

When we run above test program, we get following output.


1v3 volts using Class Adapter=3
2v12 volts using Class Adapter=12
3v120 volts using Class Adapter=120
4v3 volts using Object Adapter=3
5v12 volts using Object Adapter=12
v120 volts using Object Adapter=120
6

Adapter Pattern Class Diagram

Adapter Pattern Example in JDK

You might also like