Coordination of incompatible structures-adapter mode (2)

Source: Internet
Author: User
9.3 complete solution

Developers of Sunny software company decided to use the adapter mode to reuse algorithms in the algorithm library. Its basic structure is 9-4:

Figure 9-4 Structure of Algorithm Library Reuse

In Figure 9-4, the scoreoperation interface acts as an abstract target, the quicksort and binarysearch classes act as adaptors, And the operationadapter acts as an adapter. The complete code is as follows:

// Abstract score operation class: Target Interface interface scoreoperation {public int [] Sort (INT array []); // sort the scores by public int search (INT array [], int key ); // query results} // quick sorting class: adapter class quicksort {public int [] quicksort (INT array []) {sort (array, 0, array. length-1); Return array;} public void sort (INT array [], int P, int R) {int q = 0; If (P <R) {q = partition (array, P, R); sort (array, P, q-1); sort (array, q + 1, R );}} public int partition (INT [] A, int P, int R) {int x = A [R]; Int J = p-1; For (INT I = P; I <= R-1; I ++) {if (a [I] <= x) {J ++; swap (A, J, I) ;}} swap (, J + 1, R); Return J + 1;} public void swap (INT [] A, int I, Int J) {int T = A [I]; A [I] = A [J]; A [J] = T ;}// Binary Search Class: adapter class binarysearch {public int binarysearch (INT array [], int key) {int low = 0; int high = array. length-1; while (low <= high) {int mid = (low + high)/2; int midval = array [Mid]; If (midval <key) {LOW = Mid + 1;} else if (midval> key) {high = mid-1;} else {return 1; // If the element is found, return 1} return-1; // No element is found.-1 }}// operation adapter: adapter class operationadapter implements scoreoperation {private quicksort sortobj; // define the adapter quicksort object private binarysearch searchobj; // define the binarysearch object public operationadapter () {sortobj = new quicksort (); searchobj = new binarysearch ();} public int [] Sort (INT array []) {return sortobj. quicksort (array); // call the sorting method of the adapter class quicksort} public int search (INT array [], int key) {return searchobj. binarysearch (array, key); // call the search method of binarysearch for the adapter class }}

To make the system flexible and scalable, we introduce the tool class xmlutil and configuration file. The code of the xmlutil class is as follows:

Import javax. XML. parsers. *; import Org. w3C. dom. *; import Org. XML. sax. saxexception; import Java. io. *; Class xmlutil {// This method is used to extract a specific class name from the xml configuration file and return an instance object public static object getbean () {try {// create Document Object documentbuilderfactory dfactory = documentbuilderfactory. newinstance (); documentbuilder builder = dfactory. newdocumentbuilder (); document DOC; Doc = builder. parse (new file ("config. XML "); // get the text node containing the class name nodelist NL = Doc. getelementsbytagname ("classname"); node classnode = NL. item (0 ). getfirstchild (); string cname = classnode. getnodevalue (); // generate an Instance Object using the class name and return Class C = Class. forname (cname); object OBJ = C. newinstance (); Return OBJ;} catch (exception e) {e. printstacktrace (); return NULL ;}}}

The configuration file config. xml stores the Class Name of the adapter class. The Code is as follows:

<?xml version="1.0"?><config><className>OperationAdapter</className></config>

Write the following client test code:

Class client {public static void main (string ARGs []) {scoreoperation operation; // program operation = (scoreoperation) xmlutil for the abstract target interface. getbean (); // read the configuration file and reflect the generated object int scores [] = {,}; // define the result array int result []; int score; system. out. println ("result sorting:"); Result = operation. sort (scores); // traverses the output result for (int I: scores) {system. out. print (I + ",");} system. out. println (); system. out. println ("query score 90:"); SCOR E = operation. Search (result, 90); If (score! =-1) {system. Out. println ("locate score 90. ");} Else {system. Out. println (" No score 90 found. ");} System. Out. println (" search result 92: "); score = operation. Search (result, 92); If (score! =-1) {system. Out. println ("locate result 92. ");} Else {system. Out. println (" No score 92 found. ");}}}

Compile and run the program. The output result is as follows:

Result of sorting scores:

,

Search for score 90:

Find score 90.

Find the score 92:

No score 92 found.

The Object Adapter mode is used in this instance, and a configuration file is introduced to store the Class Name of the adapter class in the configuration file. If you need to use other sorting algorithm classes and search algorithm classes, you can add a new adapter class and use a new adapter to adapt to the new algorithm. The original code does not need to be modified. By introducing the configuration file and reflection mechanism, you can use a new adapter without modifying the client code, without modifying the source code, and comply with the "open/close principle ".

[Author: Liu Wei http://blog.csdn.net/lovelion]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.