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]

We implement a sorting class so that it can sort the elements in the array and print the array. You can design it as follows:

DataSorter. java

/***** Sorting class ** @ author jesson **/public class DataSorter {/***** Bubble sorting method ** @ param a */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 * 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 the array ** @ param a */public static void print (int [] a) {for (int I = 0; I <. 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}; DataSorter. print (a); DataSorter. bubbleSort (a); DataSorter. print (a) ;}}</span>

This DataSorter can be used to perform Bubble Sorting on the array and output the array.

  

But now, the demand has changed. We need to not only sort the integer array, but also sort the float and double arrays, this can be used to reload the sorting method to sort arrays of different types. Yes, this is a method. However, now the demand has changed, not only to sort the arrays of these general types, but also to sort other custom classes, for example, we want to sort Cat, and output, how is this done ?? Presumably, you can easily think of using overload, as shown in the following code:

Cat. java

/*** Cat class has the attribute height, weight, and toString () method ** @ author jesson **/public class Cat {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;}/*** rewrite toString () method */@ Overridepublic String toString () {return this. getHeight () + "|" + this. getWeight () ;}}</span>
DataSorter. java

/***** Sorting class ** @ author jesson **/public class DataSorter {/***** Bubble sorting method ** @ param Cat type array */public static void bubbleSort (Cat []) {for (int I =. 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 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 * array * @ param x * array subscript 1 * @ param y * array subscript 2 */private static void 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 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 * Cat type array */public static void print (Cat [] 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 (3, 3 ), new Cat (1, 1)}; DataSorter. print (a); DataSorter. bubbleSort (a); DataSorter. print ();}}
In this way, you can not only bubble the int type array, but also sort the Cat type array.

Now the problem is coming again. What should I do if I want to sort the arrays of the Dog type? Of course, I can still use the method of overloading above, but it is too troublesome, if you want to sort other types of arrays in the future, it is not necessary to reload the sorting method. Therefore, this method cannot be used. So how can we sort all types of arrays with a unified sorting method? The answer is yes.

To sort all classes, you need to define the comparison rules. Therefore, you can first compare the classes to be compared, that is, all the classes to be compared have a common feature and can be compared. Therefore, we define a Comparable interface so that all the classes to be sorted can implement this interface. In this way, modify the Cat class code in the sorting method of the DataSorter class and define the type as Object. In this way, 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 the 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;}/*** rewrite toString () method */@ Overridepublic String toString () {return this. getHeight () + "|" + this. getWeight ();}/*** the compareTO method that implements the Comparable interface returns 1: The current value is greater than the value of the object in the parameter returned-1: The current value is smaller than the value of the object in the parameter returned 0: * The current Object is equal to the Object in the parameter * // @ 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.getFood())return 1;else if (this.food < dog.getFood())return -1;elsereturn 0;}@Overridepublic String toString(){return this.food + "";}}
DataSorter. java

/***** 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 (3, 3), new Cat (1, 1)}; Dog [] a = {new Dog (3), new Dog (2), new Dog (6)}; DataSorter. print (a); DataSorter. bubbleSort (a); DataSorter. print ();}}
After this modification, DataSorter can not only sort Cat-type arrays, but also sort Dog-type arrays. In addition, if you want to sort the newly defined classes, as long as you want to implement the Comparable interface and specifically implement the compareTo () method, you can directly use DataSorter to sort newly defined arrays without modifying the DataSorter class, it achieves the purpose of code reuse.

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.