需求: person(id,name,age) 设计一个可扩展的可根据id,name,age正序倒序排序;如果name/age相同,按id的正序排序;
/**
* 实体类
* @Author dabai
* @Date 2019-08-28
*/
public class Person {
private int id;
private String name;
private int age;
public Person(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
/**
* 策略接口
* @Author dabai
* @Date 2019-08-28
*/
public interface Sorter<T> {
/**
* 根据传入参数list和比较器 排序
* @param list
* @param comparator
*/
void sort(List<T> list, Comparator<T> comparator);
/**
* 获取自然序比较器
* @return
*/
Comparator<T> getAscComparator();
/**
* 获取倒序比较器
* @return
*/
Comparator<T> getDescComparator();
}
/**
* @Author dabai
* @Date 2019-08-28
*/
public abstract class AbstractSorter<T> implements Sorter<T> {
@Override
public void sort(List<T> list, Comparator<T> comparator) {
list.sort(list,comparator);
}
}
/**
* id 排序策略
* @Author dabai
* @Date 2019-08-28
*/
public class IDSorter extends AbstractSorter<Person> {
@Override
public Comparator<Person> getAscComparator() {
return Comparator.comparingInt(Person::getId);
}
@Override
public Comparator<Person> getDescComparator() {
return Comparator.comparingInt(Person::getId).reversed();
}
}
/**
* @Author dabai
* @Date 2019-08-28
*/
public class NameSorter extends AbstractSorter<Person> {
@Override
public Comparator<Person> getAscComparator() {
return (o1, o2) -> {
if (o1.getName().compareToIgnoreCase(o2.getName()) == 0) {
return o1.getId() - o2.getId();
}
return o1.getName().compareToIgnoreCase(o2.getName());
};
}
@Override
public Comparator<Person> getDescComparator() {
return (o1, o2) -> {
if (o1.getName().compareToIgnoreCase(o2.getName()) == 0) {
return o1.getId() - o2.getId();
}
return o2.getName().compareToIgnoreCase(o1.getName());
};
}
}
/**
* @Author dabai
* @Date 2019-08-28
*/
public class AgeSorter extends AbstractSorter<Person> {
@Override
public Comparator<Person> getAscComparator() {
return (o1, o2) -> {
if (o1.getAge() == o2.getAge()) {
return o1.getId() - o2.getId();
}
return o1.getAge() - o2.getAge();
};
}
@Override
public Comparator<Person> getDescComparator() {
return (o1, o2) -> {
if (o1.getAge() == o2.getAge()) {
return o1.getId() - o2.getId();
}
return o2.getAge() - o1.getAge();
};
}
}
/**
* @Author dabai
* @Date 2019-08-28
*/
public class Client {
public static void main(String[] args) {
// 1. 组装数据
Person p1 = new Person(1, "dabai", 30);
Person p2 = new Person(2, "baye", 30);
Person p3 = new Person(10, "wangwu", 25);
Person p4 = new Person(9, "dabai", 30);
Person p5 = new Person(6, "zhaoliu", 50);
Person p6 = new Person(3, "lisi", 30);
Person p7 = new Person(4, "qinyao", 39);
List<Person> personList = Arrays.asList(p1, p2, p3, p4, p5, p6, p7);
// 2. 获取环境单例
Enviroment env = Enviroment.getInstance();
// 3. ID策略 并 调用
IDSorter idSorter = new IDSorter();
env.sort(personList, idSorter);
env.sort(personList, idSorter, false);
// 4. Name策略 并 调用
NameSorter nameSorter = new NameSorter();
env.sort(personList, nameSorter);
env.sort(personList, nameSorter, false);
// 5. Age策略 并 调用
AgeSorter ageSorter = new AgeSorter();
env.sort(personList, ageSorter);
env.sort(personList, ageSorter, false);
// 6. 遍历输出排序后的数据
personList.forEach(System.out::println);
}
}