Java Analog Comparator interface policy mode

Source: Internet
Author: User
Tags comparable

"Java analog Comparable interface" continued

First, the teacher class and the student class comparison size way is not fixed, for example teacher besides compares the title, but also may compare the seniority size, the age size and so on. Then define the comparator interface, different comparison methods to define as a Xxcomparator class, to implement comparator interface, teacher class and student class reference specific Xxcomparator comparator, you can achieve a flexible comparison mode switch. This is an example of a strategy pattern: when I want to compare size, first define a comparator comparator, but the specific comparison is implemented by the specific comparison strategy, such as Teacheragecomparator

PS: Solve the problem of the requirements of a flexible, scalable, etc., you should focus on the use of multi-state thinking.

Two, there are several categories:

1.datasorter.java

2.studnet.java

3.teacher.java

4.comparable.java

5.comparator.java

6.teacheragecomparator.java

7.studentmarkcomparaotr.java

1.datasorter.java (as in previous article)

public class Datasorter {public static void sort (comparable [] a) {int index;//holds the subscript for each comparison, the maximum value; for (int i = 1; i < A.lengt H i++) {//control outer loop Count index = 0;for (int j = 1; J <= A.length-i; j + +) {if (A[j].compareto (a[index]) = = 1) {index = j;}} Swap (A, index, a.length-i);}} private static void Swap (comparable[] A, int x, int y) {comparable tmp = a[x];a[x] = a[y];a[y] = tmp;} Output array element public static void show (comparable[] a) {for (int i = 0; i < a.length; i++) {System.out.println (a[i]);}}

  

2.studnet.java

public class Student implements comparable<student> {private int mark;private comparator<student> Comparator = new Studentmarkcomparator ();p ublic int Getmark () {return mark;} public void Setmark (int mark) {This.mark = Mark;} Public Student (int mark) {super (); this.mark = Mark;} @Overridepublic String toString () {return "student" +mark+ "";} @Overridepublic int compareTo (Student o) {return comparator.compare (this, o);}}

  

3.teacher.java

public class Teacher implements comparable<teacher> {private int title;private int age;private comparator< Teacher> comparator = new Teacheragecomparator ();p ublic int getage () {return age;} public void Setage (int.) {this.age = age;} Public Teacher (int.) {super (); this.age = age;} public int GetTitle () {return title;} public void Settitle (int title) {this.title = title;} @Overridepublic int compareTo (Teacher o) {return comparator.compare (this, o);} @Overridepublic String toString () {return "teacher--" +age+ "";}}

  

4.comparable.java

Public interface comparable<t> {public int compareTo (T o);}

  

5.comparator.java

public interface Comparator<t> {int compare (t O1, T O2);}

  

6.teacheragecomparator.java

public class Teacheragecomparator implements comparator<teacher> {@Overridepublic int compare (Teacher O1, Teacher O2) {if (O1.getage () > O2.getage ()) {return 1;} else if (o1.getage () = = O2.getage ()) {return 0;} else{return-1;}}}

  

7.studentmarkcomparaotr.java

public class Studentmarkcomparator implements comparator<student> {@Overridepublic int compare (Student O1, Student O2) {if (O1.getmark () > O2.getmark ()) return 1;else if (o1.getmark () = = O2.getmark ()) return 0;elsereturn-1;}}

  

8.test.java

public class Test {public static void main (string[] args) {//int [] a = {9,2,1,8,0,3}; Student [] ss = {New Student, new Student (+), new Student (All)};D atasorter.sort (ss);D atasorter.show (ss); Teacher [] ts = {new Teacher (Ten), New Teacher (3), new Teacher (n)};D atasorter.sort (ts);D atasorter.show (TS);}}

  

Test results

Java Analog Comparator interface policy mode

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.