Skip to content

Commit e3901d5

Browse files
committed
Implement Pattern Adapter
1 parent f7e4609 commit e3901d5

File tree

1 file changed

+111
-2
lines changed

1 file changed

+111
-2
lines changed

README.md

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,123 @@ public class PrototypePatternTest {
208208

209209
List<String> list1 = usersNew1.getEmpList();
210210
list1.remove("Pankaj");
211-
System.out.println("users List: "+users.getEmpList());
211+
System.out.println("users List: "+ users.getEmpList());
212212
System.out.println("users New List: "+list);
213213
System.out.println("users New1 List: "+list1);
214214
}
215215
}
216216
```
217217

218-
## STRUCTURAL DESIGN PATTERNS
218+
## STRUCTURAL DESIGN PATTERNS
219219

220220
Structural Patterns provide different ways to create a class structure, for example using inheritance and composition
221221
to create a large object from small objects.
222+
223+
### Adapter Pattern
224+
225+
This pattern is used in such a way that two unrelated interfaces can work together. The object that joins these unrelated interfaces is called 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.
226+
First of all we'll have two classes : Volt - to measure volts) and Socket :
227+
228+
```java
229+
230+
public class Volt {
231+
private int volts;
232+
public Volt(int v){
233+
this.volts=v;
234+
}
235+
public int getVolts() {
236+
return volts;
237+
}
238+
public void setVolts(int volts) {
239+
this.volts = volts;
240+
}
241+
}
242+
243+
244+
public class Socket {
245+
public Volt getVolt(){
246+
return new Volt(120);
247+
}
248+
249+
}
250+
```
251+
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.
252+
253+
```java
254+
public interface SocketAdapter {
255+
public Volt get120Volt();
256+
public Volt get12Volt();
257+
public Volt get3Volt();
258+
}
259+
260+
```
261+
262+
while implementing this pattern, there are two approaches : one that deals with inheritance and another one that deals with Composition.Note that they are almost the same thus, here we'll deal with one with inheritance. Let's implement out adapter class !
263+
264+
265+
```java
266+
public class SocketClassAdapterImpl extends Socket implements
267+
SocketAdapter{
268+
@Override
269+
public Volt get120Volt() {
270+
return getVolt();
271+
}
272+
@Override
273+
public Volt get12Volt() {
274+
Volt v= getVolt();
275+
return convertVolt(v,10);
276+
}
277+
@Override
278+
public Volt get3Volt() {
279+
Volt v= getVolt();
280+
return convertVolt(v,40);
281+
}
282+
private Volt convertVolt(Volt v, int i) {
283+
return new Volt(v.getVolts()/i);
284+
}
285+
}
286+
```
287+
Now let's see how all this work ! Here's a test Main function to illustrate.
288+
289+
290+
```java
291+
292+
public class AdapterPatternTest {
293+
294+
public static void main(String[] args) {
295+
testClassAdapter();
296+
297+
testAdapter();
298+
299+
//The results goes in your console :)
300+
}
301+
302+
private static void testAdapter() {
303+
SocketAdapter sockAdapter = new SocketObjectAdapterImpl();
304+
305+
Volt v3 = getVolt(sockAdapter,3);
306+
307+
Volt v12 = getVolt(sockAdapter,12);
308+
309+
Volt v120 = getVolt(sockAdapter,120);
310+
311+
System.out.println("v3 volts using Object Adapter="+v3.getVolts());
312+
System.out.println("v12 volts using Object Adapter="+v12.getVolts());
313+
System.out.println("v120 volts using Object Adapter="+v120.getVolts());
314+
}
315+
316+
private static Volt getVolt(SocketAdapter sockAdapter, int i) {
317+
switch (i){
318+
case 3: return sockAdapter.get3Volt();
319+
case 12: return sockAdapter.get12Volt();
320+
case 120: return sockAdapter.get120Volt();
321+
default: return sockAdapter.get120Volt();
322+
}
323+
324+
325+
}
326+
327+
}
328+
```
329+
330+
### Composite Pattern

0 commit comments

Comments
 (0)