Tuesday, March 20, 2012

Adapter Pattern

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

interface TargetNumberSorter{
    public int[] sortIntegerList(int[] arr);

class AdapterNumberSorter implements TargetNumberSorter{
    public int[] sortIntegerList(int[] arr){
        //convert array to List<Integer>
        List<Integer> arrList =  new ArrayList<Integer>();
        for(int i=0;i<arr.length;i++){
        //sort List<Integer> with Adaptee
        AdapteeNumberSorter ans = new AdapteeNumberSorter();
        List<Integer> sortedList = ans.sortIntegerList(arrList);
        int sortedArr[] =new int[sortedList.size()];
        //convert List<Integer>
        for(int index=0; index<sortedList.size();index++){
            sortedArr[index] = sortedList.get(index);
         return sortedArr;

class AdapteeNumberSorter{
    public List<Integer> sortIntegerList(List<Integer> inputList){
        return inputList;

class ApplicationClient{
    public static void main(String[]args){
        int arr[]={45,10,1,46,98,22,30,8};
        System.out.println("array before sorting Data ");
        for(int i=0;i<arr.length;i++){
            System.out.println("index ["+i+"] = "+arr[i]);
        System.out.println("array after sorting Data ");
        TargetNumberSorter tns =  new AdapterNumberSorter();
        int sortredArr[] = tns.sortIntegerList(arr);
        for(int i=0;i<sortredArr.length;i++){
            System.out.println("index ["+i+"] = "+sortredArr[i]);

No comments:

Post a Comment