[This is my own study notes, welcome reprint, but please specify the source:http://blog.csdn.net/jesson20121020]
We implement a sort class that enables the ordering of elements in an array, as well as the ability to print arrays. As shown below, you can design this:
Datasorter.java
/** * Sort class * * @author Jesson * */public class Datasorter {/** * bubble Sort method * * @param a */public static void bubbl Esort (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 * @param x * array subscript 1 * @param y * array subscript 2 */private static void Swap (int[] A, I NT x, int y) {//TODO auto-generated method Stubint temp = a[x];a[x] = a[y];a[y] = temp;} /** * Print Array * * @param a */public static void print (int[] a) {for (int i = 0; i < a.length; i++) System.out.print (a[i ] + " "); System.out.println ();}} </span>
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};D Atasorter.print (a);D Atasorter.bubblesort (a);D atasorter.print (a);}} </span>
This datasorter enables the ability to bubble-sort arrays and output arrays.
But now, the demand has changed, we not only want to implement the array of integers to sort, but also for the float type and double array to sort, of course, it must be thought, this can overload the sorting method to achieve different types of array to sort, yes, this is a method. But now the demand has changed, not only to sort the arrays of these regular types, but also to sort some other custom classes, such as, we want to sort the cat, and output, how is this done?? Presumably it is easy to think of using overloading, as below, to write:
Cat.java
/** * Cat class has attribute height, weight and ToString () method * * @author Jesson * */public class Cat {private int height;//height private int Wei Ght Weight 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 ();}} </span>
Datasorter.java
/** * Sort class * * @author Jesson * */public class Datasorter {/** * Bubble Sort Method * * @param cat type array */public static void Bubbleso RT (Cat[] a) {for (int i = a.length-1; I >= 1, i--) {for (int j = 0; J < i; J + +) {if (A[j].getheight () > a[j + 1 ].getheight ()) {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 * @param x * Array subscript 1 * @param y * array subscript 2 */private static VO ID swap (cat[] A, int x, int y) {//TODO auto-generated method Stubcat 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 * cat type array */puBlic static void print (cat[] 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 (3,3), new Cat (N)};D Atasorter.print (a);D Atasorter.bubblesort (a);D atasorter.print (a);}
This allows you to not only bubble-sort an array of type int, but also to sort an array of cat types.
Now the problem again, the need to change, to achieve the dog type array to sort, how to do it, of course, or can be overloaded with the method above, but too troublesome, if later on the other types of array to sort, not to be overloaded sorting method, so, this method is not advisable. So how can you sort all types of arrays with a uniform sorting method? The answer is yes.
In order for all classes to be sorted, it is necessary to define the rules for comparison, so the classes to be compared can be compared first, that is, all the classes to be compared have a common feature to compare, so we define a comparable interface that implements this interface for classes that need to be sorted. In this way, we then modify the code in the sorting method in the Datasorter class to sort the cat class, and the type is defined as object, so that all classes can be sorted, as follows:
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 ; Height private int weight; Weight 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 ();} /** * The CompareTo method that implements the comparable interface returns 1: The current is larger than the object in the argument returns 1: The current is smaller than the object in the argument returns 0: * The current and Parameter objects are equal//@Overridepublic int CompareTo ( Object o) {//TODO auto-generated method Stubcat cat = (cat) o;if (This.height > Cat.getheight ()) return 1;else if (this . Height < Cat.getheight ()) Return-1;elsereturn 0;}}
Dog.java
public class Dog implements comparable {private int food;public Dog (int. food) {super ();//TODO Auto-generated constructor Stubthis.food = food;} public int Getfood () {return food;} public void Setfood (int. food) {This.food = food;} @Overridepublic int compareTo (Object o) {//TODO auto-generated method Stubdog dog = (dog) o;if (This.food > Dog.getfoo D ()) return 1;else if (This.food < Dog.getfood ()) Return-1;elsereturn 0;} @Overridepublic String toString () {return this.food + "";}}
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 (3,3), New Cat ()};D og[] A = {new Dog (3), New Dog (2), New Dog (6)};D Atasorter.print (a) ;D Atasorter.bubblesort (a);D atasorter.print (a);}}
After this modification, datasorter can not only sort the array of cat types, but also sort the array of the dog type, and, at this point, if you need to sort the newly defined class, just let it implement the comparable interface, and implement CompareTo () method, you can not change the Datasorter class, you can directly use Datasorter to the new definition of the type of array to order, to achieve the purpose of code reuse.
Java design pattern--------mode of action (1)