Java-马士兵设计模式学习笔记-策略模式-模拟 Comparator接口
续上一篇 《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; } @Override public String toString() { return "student" +mark+" "; } @Override public 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; } @Override public int compareTo(Teacher o) { return comparator.compare(this, o); } @Override public 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> { @Override public 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> { @Override public int compare(Student o1, Student o2) { if(o1.getMark() > o2.getMark()) return 1; else if(o1.getMark() == o2.getMark()) return 0; else return -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); } }
测试结果
You can do anything you set your mind to, man!