[This is my own study notes, welcome reprint, but please specify the source:http://blog.csdn.net/jesson20121020]
To continue with the last section, or to sort all sorts of arrays, let's consider a bit more complicated, if we don't sort by cat's height, we want to sort by the age of cat, and then, like, we no longer sort by dog's food, But the dog's weight as the basis to sort, how to do it, of course, we can change the cat class, dog class, let it implement these functions, but when the class is many, to change each class, it is also very troublesome, that is, the current structure is not good implementation. So, we want to be able to specify the basis of comparison flexibly, so it is easy to think that this is going to use polymorphism, we can define a comparator interface comparator, and then, As needed, we can customize each type of comparator, such as Catheightcomparator and Catweightcomparator, to implement a common comparator interface, and finally, we just need to change the cat class's CompareTo () method on the line, the implementation of the following:
Comparator.java
/** * Comparator * @author Jesson * */public interface Comparator {int Compore (Object o1,object O2);}
Catheightcomparator.java
/** * Cat Type Height Comparator * * @author Jesson * */public class Catheightcomparator implements Comparator {@Overridepublic in T Compore (Object O1, Object O2) {//TODO auto-generated method Stubcat C1 = (Cat) O1; Cat C2 = (cat) o2;if (C1.getheight () > C2.getheight ()) return 1;else if (C1.getheight () < C2.getheight ()) Return-1;el Sereturn 0;}}
Catweightcomparator.java
/** * Cat Type Weight Comparator * @author Jesson * */public class Catweightcomparator implements Comparator {@Overridepublic int compore ( Object O1, Object O2) {//TODO auto-generated method Stubcat C1 = (Cat) O1; Cat C2 = (cat) o2;if (C1.getweight () > C2.getweight ()) Return-1;else if (C1.getweight () < C2.getweight ()) return 1;el Sereturn 0;}}
Comparable.java
/** * Comparable interface * Defines a comparison method * @author Jesson * */public interface comparable {public int compareTo (Object o);}
Cat.java
/** * Cat class has attribute height, weight and ToString () method * * @author Jesson * */public class Cat implements comparable {private int height;//Body High private int weight; Weight//private Comparator Comparator = new Catheightcomparator (); Height Comparator private Comparator 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;} /** * Override ToString () method */@Overridepublic String ToString () {return this.getheight () + "|" + This.getweight ();} /** * Implement the CompareTo method of the comparable interface * Call the specific comparator to compare the size */@Overridepublic int compareTo (Object o) {return Comparator.compore (t His, o);}}
Datasorter.java
/** * Sort class * * @author Jesson * */public class Datasorter {/** * Bubble Sort Method * * @param cat type array */public static void Bubbleso RT (Object[] a) {for (int i = a.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 Sort Method * * @param a * integer array */public static void Bubblesort (int[] a) {for (int i = a.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 * array of type Object * @param x * Array subscript 1 * @param y * Array subscript 2 */private s tatic void Swap (object[] A, 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 VO ID 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 < a.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 < a.length; i++) Sy Stem.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 (UP), New Cat (3,3)};//dog[] A = {new Dog (3), New Dog (2), New Dog (6)};D Atasorter.print (a);D ATA Sorter.bubblesort (a);D atasorter.print (a);}}
With a high comparator and a weight comparator, we can flexibly choose the basis for comparison, at which point, if the requirements change, according to Cat age to sort, only need to create a new age comparator, The new Age comparator is then passed to the Cat class's comparator set method or cat's construction method, so that the cat type array can be sorted by the newly defined age without the need to change other code, maximizing the reuse of the code.
Java design pattern--------mode of action (2)