Table of Contents
Real life example:
A very simple example is using phone charger. Suppose your mobile phone needs 9 Volts of supply to get charged but main supply is 220 V which is not what you require but it can be a source and you can have something that can get 9 V out of this 220 V and supply to your phone for charging.
Now phone charger is acting as an adapter for you.  So Basically it is making a relationship between two unrelated interfaces. This  is how Adpater pattern works.
Intent:
Also known as:
Wrapper
UML diagram of generic adapter design pattern:
Elements:
- Target
- defines domains-specific interface client uses.
- Client
- collaborates with objects conforming to target interface.
- Adaptee
- defines existing interface that needs adapting
- Adapter
- adapts the interface of adaptee to target interface.
WorkFlow:
Clients call operations on the adapter instance.In turn adapter calls adaptee operations that carry out the request.
When to use it:
- You want to use existing class and its interface does not match the one you need.
- You want to create a resuable class that cooperates with unrelated or unforeseen classes that is, class that don’t necessarily have compatible interfaces.
Example:
Comparing to above generic description of adapter pattern.
- PrintableList(Target)
- PrintString(Adaptee)
- PrintableListAdapter(Adapter)
Java code for all above classes:
Consider that we have a third party library that provides print string functionality through PrintString class.
This is our Adaptee. I know this is silly assumpsion but lets go with it for now.
PrintString.java(Adaptee) :
1 2 3 4 5 6 7 8 9 10 11 |
package org.arpit.javapostsforlearning.designpatterns; public class PrintString { public void print(String s) { System.out.println(s); } } |
Client deals with ArrayList but not with string.We have provided a PrintableList interface that expects the client input.This is our target.
PrintableList.java(Target):
1 2 3 4 5 6 7 8 9 |
package org.arpit.javapostsforlearning.designpatterns; import java.util.ArrayList; public interface PrintableList { void printList(ArrayList list); } |
Finally we have PrintableListAdapter class which will implement PrintableList interface and will deal with our adaptee class.
PrintableListAdapter.java(Adapter):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package org.arpit.javapostsforlearning.designpatterns; import java.util.ArrayList; public class PrintableListAdapter implements PrintableList{ public void printList(ArrayList list) { //Converting ArrayList to String so that we can pass String to // adaptee class String listString = ""; for (String s : list) { listString += s + "t"; } // instantiating adaptee class PrintString printString=new PrintString(); ps.print(listString); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package org.arpit.javapostsforlearning.designpatterns; import java.util.ArrayList; public class AdapterDesignPatternMain { public static void main(String[] args) { ArrayList list=new ArrayList(); list.add("one"); list.add("two"); list.add("three"); PrintableList pl=new PrintableListAdapter(); pl.printList(list); } } |
Output:
1 2 3 |
one two three |