Home > Java > Adapter Pattern

Adapter Pattern

December 20, 2015 Leave a comment Go to comments

Adapter pattern work as middle-man between two incompatible interface. This involve a class that has responbility to join functionality incompatible interface, that class is called adapter.


Adapter Design Pattern

Adapter Design Pattern


Target interface is interface that client used, so client only know about that interaface to interact with. Adaptee is the existing interface that needs adapting in order for our client to interact with it, it can be third party library, etc. The adapter translate the Adaptee to the target interface. Here is example.


import java.util.Collections;
import java.util.List;

public class NumberSorter {
 public List<Integer> sort(List<Integer> numbers) {
 return numbers;


public interface Sorter {
 public int[] sort(int[] numbers);


import java.util.ArrayList;
import java.util.List;

public class SortListAdapter implements Sorter {

 private List<Integer> listNumbers;
 private int[] arrayNumbers;
 private NumberSorter numberSorter;

 public int[] sort(int[] numbers) {
 numberSorter = new NumberSorter();
 return convertListToArray(numberSorter.sort(convertArrayToList(numbers)));

 private List<Integer> convertArrayToList(int[] numbers) {
 listNumbers = new ArrayList<Integer>();
 for (int i = 0; i < numbers.length; i++) {
 return listNumbers;

 private int[] convertListToArray(List<Integer> numbers) {
 arrayNumbers = new int[numbers.size()];
 for (int i = 0; i < numbers.size(); i++) {
 arrayNumbers[i] = numbers.get(i);
 return arrayNumbers;


public class Client implements Sorter {

 private SortListAdapter adapter;

 public int[] sort(int[] numbers) {
 adapter = new SortListAdapter();
 return adapter.sort(numbers);

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: