IT. Expert System.

Design patterns

Adapter


Adapter 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).

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; } }
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.

public interface SocketAdapter { public Volt get120Volt(); public Volt get12Volt(); public Volt get3Volt(); }

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.

//Using inheritance for adapter pattern
public class SocketClassAdapterImpl extends Socket implements SocketAdapter{ @Override public Volt get120Volt() { return getVolt(); } @Override public Volt get12Volt() { Volt v= getVolt(); return convertVolt(v,10); } @Override public Volt get3Volt() { Volt v= getVolt(); return convertVolt(v,40); } private Volt convertVolt(Volt v, int i) { return new Volt(v.getVolts()/i); } }

Object Adapter Implementation

Here is the Object adapter implementation of our adapter.

public class SocketObjectAdapterImpl implements SocketAdapter{ //Using Composition for adapter pattern private Socket sock = new Socket(); @Override public Volt get120Volt() { return sock.getVolt(); } @Override public Volt get12Volt() { Volt v= sock.getVolt(); return convertVolt(v,10); } @Override public Volt get3Volt() { Volt v= sock.getVolt(); return convertVolt(v,40); } private Volt convertVolt(Volt v, int i) { return new Volt(v.getVolts()/i); } }

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.

public class AdapterPatternTest { public static void main(String[] args) { testClassAdapter(); testObjectAdapter(); } private static void testObjectAdapter() { SocketAdapter sockAdapter = new SocketObjectAdapterImpl(); Volt v3 = getVolt(sockAdapter,3); Volt v12 = getVolt(sockAdapter,12); Volt v120 = getVolt(sockAdapter,120); System.out.println("v3 volts using Object Adapter="+v3.getVolts()); System.out.println("v12 volts using Object Adapter="+v12.getVolts()); System.out.println("v120 volts using Object Adapter="+v120.getVolts()); } private static void testClassAdapter() { SocketAdapter sockAdapter = new SocketClassAdapterImpl(); Volt v3 = getVolt(sockAdapter,3); Volt v12 = getVolt(sockAdapter,12); Volt v120 = getVolt(sockAdapter,120); System.out.println("v3 volts using Class Adapter="+v3.getVolts()); System.out.println("v12 volts using Class Adapter="+v12.getVolts()); System.out.println("v120 volts using Class Adapter="+v120.getVolts()); } private static Volt getVolt(SocketAdapter sockAdapter, int i) { switch (i){ case 3: return sockAdapter.get3Volt(); case 12: return sockAdapter.get12Volt(); case 120: return sockAdapter.get120Volt(); default: return sockAdapter.get120Volt(); } } }

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

		
v3 volts using Class Adapter=3
v12 volts using Class Adapter=12
v120 volts using Class Adapter=120
v3 volts using Object Adapter=3
v12 volts using Object Adapter=12
v120 volts using Object Adapter=120

Adapter Pattern Class Diagram

adapter-pattern-java-class-diagram

Adapter Pattern Example in JDK

  • java.util.Arrays#asList()
  • java.io.InputStreamReader(InputStream) (returns a Reader)
  • java.io.OutputStreamWriter(OutputStream) (returns a Writer)


Content

Android Reference

Java basics

Java Enterprise Edition (EE)

Java Standard Edition (SE)

SQL

HTML

PHP

CSS

Java Script

MYSQL

JQUERY

VBS

REGEX

C

C++

C#

Design patterns

RFC (standard status)

RFC (proposed standard status)

RFC (draft standard status)

RFC (informational status)

RFC (experimental status)

RFC (best current practice status)

RFC (historic status)

RFC (unknown status)

IT dictionary

License.
All information of this service is derived from the free sources and is provided solely in the form of quotations. This service provides information and interfaces solely for the familiarization (not ownership) and under the "as is" condition.
Copyright 2016 © ELTASK.COM. All rights reserved.
Site is optimized for mobile devices.
Downloads: 1353 / . Delta: 0.01944 с