Click here to Skip to main content
13,299,495 members (71,731 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 9 Oct 2012

Adapter Design Pattern in Java

, 9 Oct 2012
Rate this:
Please Sign up or sign in to vote.
CodeProjectReal life example: A very simple example is using phone charger.

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.


As described by Gof:

"Convert the interface of class into another interface clients expect.Adapter lets class work together that couldn't otherwise because of incompatible interfaces".

Also known as: Wrapper

UML Diagram of Generic Adapter Design Pattern


  • 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. 


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.


UML Diagram for example

Comparing to above generic description of adapter pattern. My example includes following elements:

  • 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 assumption but lets go with it for now. :

package org.arpit.javapostsforlearning.designpatterns;

public class PrintString {

	public void print(String s)

Client deals with ArrayList<String> but not with string.We have provided a PrintableList interface that expects the client input. This is our target.

package org.arpit.javapostsforlearning.designpatterns;

import java.util.ArrayList;

public interface PrintableList {
	void printList(ArrayList<String> list);

Let's assume we can not change it now. Finally we have PrintableListAdapter class which will implement PrintableList interface and will deal with our adaptee class.

package org.arpit.javapostsforlearning.designpatterns;

import java.util.ArrayList;

public class PrintableListAdapter implements PrintableList{

	public void printList(ArrayList<String> list) {
		//Converting ArrayList<String> 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();

package org.arpit.javapostsforlearning.designpatterns;

import java.util.ArrayList;
public class AdapterDesignPatternMain {
	public static void main(String[] args)
		ArrayList<String> list=new  ArrayList<String>();
		PrintableList pl=new PrintableListAdapter();


one	two	three


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

I am a java developer and blogger.Love to connect with people interested in java,programming or blogging.You can visit my blog at

You may also be interested in...

Comments and Discussions

QuestionMy vote of 5 Pin
Member 784898421-Apr-14 5:54
memberMember 784898421-Apr-14 5:54 
AnswerRe: My vote of 5 Pin
Arpit Mandliya23-Apr-14 1:28
memberArpit Mandliya23-Apr-14 1:28 
GeneralMy vote of 5 Pin
Patrick Wanjau1-Nov-12 23:11
memberPatrick Wanjau1-Nov-12 23:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 9 Oct 2012
Article Copyright 2012 by Arpit Mandliya
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid