Java 類比 Comparator介面 策略模式

來源:互聯網
上載者:User

標籤:

續上一篇  《Java 類比 Comparable介面》

一、Teacher類及Student類的比較大小方式是不固定的,比如老師除了比較職稱外,還可比較工齡大小,年齡大小等。則定義Comparator介面,不同比較方法去定義為一個xxComparator類,去實現Comparator介面,Teacher類及Student類引用具體的xxComparator比較子,則可實現靈活的比較方式切換。此則為策略模式的一個例子:當我要比較大小的時候,先定義一個比較子Comparator,但具體的比較由具體的比較策略去實現,如TeacherAgeComparator

PS:解決的問題一涉及要求靈活多變,可擴充等進,則應重點考慮使用多態思想。

 

二、有如下幾個類:

1.DataSorter.java

2.Studnet.java

3.Teacher.java

4.Comparable.java

5.Comparator.java

6.TeacherAgeComparator.java

7.StudentMarkComparaotr.java

 

1.DataSorter.java(和上一篇一樣)

public class DataSorter {public static void sort(Comparable [] a) {int index;//儲存每次比較,最大值的下標;for(int i = 1; i < a.length; i++){//控制外迴圈次數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;}//輸出數組元素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();public 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();public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Teacher(int age) {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(59),new Student(30),new Student(90)};DataSorter.sort(ss);DataSorter.show(ss);Teacher [] ts = {new Teacher(10),new Teacher(3),new Teacher(12)};DataSorter.sort(ts);DataSorter.show(ts);}}

  

測試結果

Java 類比 Comparator介面 策略模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.