Person类
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 int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age != other.age)
return false;
if (id != other.id)
return false;
if (name == null)
{
if (other.name != null)
return false;
}
else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString()
{
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
策略的公共接口
import java.util.List;
public interface PersonSort
{
public void sort(List<Person> list);
}
策略类
1.按ID升序排序
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class IdUpsort implements PersonSort,Comparator<Person>
{
@Override
public void sort(List<Person> list)
{
Collections.sort(list,this);
}
public int compare(Person o1, Person o2)
{
return o1.getId()-o2.getId();
}
}
按ID降序排序
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class IdDnsort implements Comparator<Person>, PersonSort
{
@Override
public void sort(List<Person> list)
{
Collections.sort(list,this);
}
public int compare(Person o1, Person o2)
{
return o2.getId()-o1.getId();
}
}
2.按年龄升序排序
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class AgeUpsort implements PersonSort,Comparator<Person>
{
@Override
public void sort(List<Person> list)
{
Collections.sort(list,this);
}
@Override
public int compare(Person o1, Person o2)
{
int result = o1.getAge()-o2.getAge();
if(result==0)
{
return o1.getId()-o2.getId();
}
return result;
}
}
按年龄降序排序
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class AgeDnsort implements Comparator<Person>, PersonSort
{
@Override
public void sort(List<Person> list)
{
Collections.sort(list,this);
}
@Override
public int compare(Person o1,Person o2)
{
int result = o2.getAge()-o1.getAge();
if(result==0)
{
return o1.getId()-o2.getId();
}
return result;
}
}
3.按名字升序排序
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class NameUpsort implements PersonSort,Comparator<Person>
{
@Override
public void sort(List<Person> list)
{
Collections.sort(list,this);
}
@Override
public int compare(Person o1, Person o2)
{
int result = o1.getName().compareTo(o2.getName());
if(result==0)
{
return o1.getId()-o2.getId();
}
return result;
}
}
按名字降序排序
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class NameDnsort implements PersonSort, Comparator<Person>
{
@Override
public void sort(List<Person> list)
{
Collections.sort(list,this);
}
@Override
public int compare(Person o1, Person o2)
{
int result = o2.getName().compareTo(o1.getName());
if(result==0)
{
return o1.getId()-o2.getId();
}
return result;
}
}
封装策略类的环境类
import java.util.List;
public class Environment
{
private PersonSort personsort;
public Environment(PersonSort personsort)
{
this.personsort = personsort;
}
public PersonSort getPersonsort()
{
return personsort;
}
public void setPersonsort(PersonSort personsort)
{
this.personsort = personsort;
}
public void sort(List<Person> list)
{
personsort.sort(list);
}
}
测试类
import java.util.ArrayList;
import java.util.List;
public class StrategyTest
{
public static void main(String[] args)
{
Person p1 = new Person(201001,"zhangsan",24);
Person p2 = new Person(201004,"lisi",19);
Person p3 = new Person(201003,"zhangsan",25);
Person p4 = new Person(201002,"zhaoliu",27);
Person p5 = new Person(201006,"lisi",24);
Person p6 = new Person(201005,"kuroro",24);
List<Person> list = new ArrayList<Person>();
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
list.add(p5);
list.add(p6);
System.out.println("排序前状态:");
System.out.println(list);
System.out.println("-----------------------");
Environment en = new Environment(new IdUpsort());
en.sort(list);
System.out.println("按ID正序排列:");
System.out.println(list);
en.setPersonsort(new IdDnsort());
en.sort(list);
System.out.println("按ID逆序排列:");
System.out.println(list);
System.out.println("-----------------------");
en.setPersonsort(new AgeUpsort());
en.sort(list);
System.out.println("按年龄正序排列:");
System.out.println(list);
en.setPersonsort(new AgeDnsort());
en.sort(list);
System.out.println("按年龄逆序排列:");
System.out.println(list);
System.out.println("-----------------------");
en.setPersonsort(new NameUpsort());
en.sort(list);
System.out.println("按名字正序排列:");
System.out.println(list);
en.setPersonsort(new NameDnsort());
en.sort(list);
System.out.println("按名字逆序排列:");
System.out.println(list);
}
}
打印结果
排序前状态:
[Person [id=201001, name=zhangsan, age=24], Person [id=201004, name=lisi, age=19], Person [id=201003, name=zhangsan, age=25], Person [id=201002, name=zhaoliu, age=27], Person [id=201006, name=lisi, age=24], Person [id=201005, name=kuroro, age=24]]
-----------------------
按ID正序排列:
[Person [id=201001, name=zhangsan, age=24], Person [id=201002, name=zhaoliu, age=27], Person [id=201003, name=zhangsan, age=25], Person [id=201004, name=lisi, age=19], Person [id=201005, name=kuroro, age=24], Person [id=201006, name=lisi, age=24]]
按ID逆序排列:
[Person [id=201006, name=lisi, age=24], Person [id=201005, name=kuroro, age=24], Person [id=201004, name=lisi, age=19], Person [id=201003, name=zhangsan, age=25], Person [id=201002, name=zhaoliu, age=27], Person [id=201001, name=zhangsan, age=24]]
-----------------------
按年龄正序排列:
[Person [id=201004, name=lisi, age=19], Person [id=201001, name=zhangsan, age=24], Person [id=201005, name=kuroro, age=24], Person [id=201006, name=lisi, age=24], Person [id=201003, name=zhangsan, age=25], Person [id=201002, name=zhaoliu, age=27]]
按年龄逆序排列:
[Person [id=201002, name=zhaoliu, age=27], Person [id=201003, name=zhangsan, age=25], Person [id=201001, name=zhangsan, age=24], Person [id=201005, name=kuroro, age=24], Person [id=201006, name=lisi, age=24], Person [id=201004, name=lisi, age=19]]
-----------------------
按名字正序排列:
[Person [id=201005, name=kuroro, age=24], Person [id=201004, name=lisi, age=19], Person [id=201006, name=lisi, age=24], Person [id=201001, name=zhangsan, age=24], Person [id=201003, name=zhangsan, age=25], Person [id=201002, name=zhaoliu, age=27]]
按名字逆序排列:
[Person [id=201002, name=zhaoliu, age=27], Person [id=201001, name=zhangsan, age=24], Person [id=201003, name=zhangsan, age=25], Person [id=201004, name=lisi, age=19], Person [id=201006, name=lisi, age=24], Person [id=201005, name=kuroro, age=24]]