Design Patterns - Adapter Pattern

An existing interface DataOperation defines the sorting method sort(int[])  and the search method search(int[], int) . It is known that the quickSort (int[]) method of the class QuickSort  implements the quicksort algorithm, and the binarySearch( int[], int) method implements the binary search algorithm. Try to use the adapter pattern to design a system that adapts the methods of the class QuickSort and the class BinarySearch to the DataOperation interface without modifying the source code . Draw a class diagram and implement it programmatically. (requires to implement quick sort and binary search, implemented using object adapter)

Class Diagram:

 

Implementation code:

Target abstract class DataOperation :

public interface DataOperation {

    public void sort(int[] a,int low,int high);

    public int search(int [] srcArray, int start, int end, int key);

}

Adapter class QuickSort :

public class QuickSort {

    public  void quickSout(int[] a,int low,int high){

            int start = low;

            int end = high;

            int key = a[low];

            while(end>start){

                // Compare from back to front

                while(end>start&&a[end]>=key) // If there is no smaller than the key value, compare the next one until there is an exchange position smaller than the key value, and then compare from front to back

                    end--;

                if(a[end]<=key){

                    int temp = a[end];

                    a[end] = a[start];

                    a[start] = temp;

                }

                // Compare from front to back

                while(end>start&&a[start]<=key)// If there is no larger than the key value, compare the next one until there is an exchange position larger than the key value

                    start++;

                if(a[start]>=key){

                    int temp = a[start];

                    a[start] = a[end];

                    a[end] = temp;

                }

                //此时第一次循环比较结束,关键值的位置已经确定了。左边的值都比关键值小,右边的值都比关键值大,但是两边的顺序还有可能是不一样的,进行下面的递归调用

            }

            //递归

            if(start>low) quickSout(a,low,start-1);//左边序列。第一个索引位置到关键值索引-1

            if(end<high) quickSout(a,end+1,high);//右边序列。从关键值索引+1到最后一个

        }

 

    }

适配者类BinarySearch

public class BinarySearch {

    public int binarySearch(int srcArray[], int start, int end, int key){

        int mid = (end - start) / 2 + start;

        if (srcArray[mid] == key) {

            return mid;

        }

        if (start >= end) {

            return -1;

        } else if (key > srcArray[mid]) {

            return binarySearch(srcArray, mid + 1, end, key);

        } else if (key < srcArray[mid]) {

            return binarySearch(srcArray, start, mid - 1, key);

        }

        return -1;

    }

}

 

适配器类DataOpAdapter

public class DataOpAdapter implements DataOperation {

    private QuickSort qSort;

    private BinarySearch binarySearch;

 

 

public DataOpAdapter(QuickSort qSort,BinarySearch binarySearch) {

    this.qSort=qSort;

    this.binarySearch=binarySearch;

}

    @Override

    public int search(int [] srcArray, int start, int end, int key){

 

        return  binarySearch.binarySearch(srcArray,start,end,key);

    }

 

    @Override

    public void sort(int[] a,int low,int high) {

        qSort.quickSout(a,low,high);

    }

}

 

 

 

客户端类Main

public class Main {

 

    public static void main(String[] args) {

        DataOperation dataOperation =new DataOpAdapter(new QuickSort(), new BinarySearch());

        int[] a = {12,20,5,16,15,1,30,45,23,9};

        System.out.println("排序前:");

        for(int i = 0; i<a.length; i++){

            System.out.print(a[i]+" ");

        }

        int start = 0;

        int end = a.length-1;

        dataOperation.sort(a,start,end);

        System.out.println("\n"+"实现快速排序:");

        for(int i = 0; i<a.length; i++){

            System.out.print(a[i]+" ");

        }

        System.out.println("\n"+"实现了二分查找算法,查找“20");

        System.out.println(dataOperation.search(a,0,a.length -1,20));

    }

}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324734203&siteId=291194637