Java design mode, java design mode pdf

Source: Internet
Author: User

Java design mode, java design mode pdf

[This article is my own learning notes. You are welcome to repost it, but please note the Source: http://blog.csdn.net/jesson20121020]

Continue with the content in the previous section. As of now, the sorting of various types of arrays has been achieved, and the scalability is also good. We have defined the comparator interface, we can adapt to different comparison strategies, but we note that when implementing the compare () method, we need to perform a downward transformation to convert the Object type to a specific type, which is still a little troublesome, is there a way for us to specify the type to be compared when implementing the Comparator interface? At this time, we can easily think that the <T> generic type in JDK can implement this function. Therefore, we apply the <T> wildcard to our Comparator. We apply the Comparator and Comparable in JDK to our program, as shown below:

Cat. java

Import java. util. comparator;/*** Cat class has the attribute height, weight, and toString () method ** @ author jesson **/public class Cat implements java. lang. comparable <Cat> {private int height; // private int weight; // weight // private Comparator comparator = new CatHeightComparator (); // height Comparator private comparator <Cat> Comparator = new CatWeightComparator (); // weight comparator public Comparator getComparator () {return comparator;} public void setComparator (Comparator comparator) {this. comparator = comparator;} public Cat (int height, int weight) {// TODO Auto-generated constructor stubthis. height = height; this. weight = weight;} public int getHeight () {return height;} public void setHeight (int height) {this. height = height;} public int getWeight () {return weight;} public void setWeight (int weight) {this. weight = weight;}/*** rewrite toString () method */@ Overridepublic String toString () {return this. getHeight () + "|" + this. getWeight () ;}/ *** compareTO method implementing the Comparable interface * call a specific comparator to compare the size */@ Overridepublic int compareTo (Cat o) {return comparator. compare (this, o );}}
CatHeightComparator. java

Import java. util. comparator;/*** Cat type height Comparator ** @ author jesson **/public class CatHeightComparator implements Comparator <Cat >{@ Overridepublic int compare (Cat o1, Cat o2) {// TODO Auto-generated method stubif (o1.getHeight ()> o2.getHeight () return 1; else if (o1.getHeight () <o2.getHeight () return-1; elsereturn 0 ;}}
CatWeightComparator. java

Import java. util. comparator;/*** Cat type weight Comparator ** @ author jesson **/public class CatWeightComparator implements Comparator <Cat >{@ Overridepublic int compare (Cat o1, Cat o2) {// TODO Auto-generated method stubif (o1.getWeight ()> o2.getWeight () return-1; else if (o1.getWeight () <o2.getWeight () return 1; elsereturn 0 ;}}
DataSorter
Import java. lang. comparable; /***** sorting class ** @ author jesson **/public class DataSorter {/***** Bubble sorting method ** @ param Cat type array */public static void bubbleSort (Object []) {for (int I =. length-1; I> = 1; I --) {for (int j = 0; j <I; j ++) {Comparable o1 = (Comparable) a [j]; comparable o2 = (Comparable) a [j + 1]; if (o1.compareTo (o2) = 1) {swap (a, j, j + 1 );}}}} /*** Bubble sorting method ** @ param a * integer array */public static void bubbleSort (int [] a) {for (int I =. length-1; I> = 1; I --) {for (int j = 0; j <I; j ++) {if (a [j]> a [j + 1]) {swap (a, j, j + 1 );}}}} /*** exchange two data ** @ param a * Object type array * @ param x * array subscript 1 * @ param y * array subscript 2 */private static void swap (Object [], int x, int y) {// TODO Auto-generated method stubObject temp = a [x]; a [x] = a [y]; a [y] = temp ;} /*** exchange two data ** @ param a * array * @ param x * array subscript 1 * @ param y * array subscript 2 */private static void swap (int [] a, int x, int y) {// TODO Auto-generated method stubint temp = a [x]; a [x] = a [y]; a [y] = temp ;} /*** print array ** @ param a * Object type array */public static void print (Object [] a) {for (int I = 0; I <. length; I ++) System. out. print (a [I] + ""); System. out. println ();}/*** print array ** @ param a * int type array */public static void print (int [] a) {for (int I = 0; I <. length; I ++) System. out. print (a [I] + ""); System. out. println ();}}
Test. java

/*** Test class ** @ author jesson **/public class Test {public static void main (String [] args) {// int [] a = new int [] {9, 8, 2, 4, 5, 6, 7}; Cat [] a = {new Cat (5, 5), new Cat ), new Cat (3, 3)}; // Dog [] a = {new Dog (3), new Dog (2), new Dog (6)}; DataSorter. print (a); // DataSorter. bubbleSort (a); java. util. arrays. sort (a); DataSorter. print ();}}
We can see that the Comparable and Comparator defined by ourselves are not used here, but the Comparable <T> and Comparator <T> interfaces in JDK are used. In fact, JDK has encapsulated the sort () method, we use it directly, but there is a premise that the class to be sorted must implement the JDK Comparable interface; so in the test program, we can directly use the sort (Object [] o) method in JDK to sort the arrays of the defined types.

As mentioned above, the Comparable and Comparator interfaces we wrote are only used to simulate the corresponding interfaces in JDK. In actual development, we can use them directly.

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.