一 策略模式的描述
二 实现策略模式的简单例子
问题描述 :
代码 :
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Random;public class StrategyPattern {
/**
*
* 此方法用于随机生成一个字符串,用于人的姓名
* @param length
* @return
*/
public static String getRandomString(int length) { // length表示生成字符串的长度
String base = "abcdefghijklmnopqrstuvwxyz"; // 生成字符串从此序列中取
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString();
}public static void main(String[] args) {
LinkedList list = new LinkedList(); //存放人的信息
for (int i = 0; i < 50; i++) { //随机生成50个人的信息
String id = "1000" + i;
String name = StrategyPattern.getRandomString(6);
int age = new Random().nextInt(80);
list.add(new Person(id, name, age));
}
//生成一个ID 比较器
PersonComparator personComparator = new PersonComparator(new IDComparator() );
Collections.sort(list, personComparator); //人按照ID进行排序
System.out.println(list);
personComparator.setComparator(new NameComparator());
Collections.sort(list, personComparator); //人按照Name进行排序
System.out.println(list);
personComparator.setComparator(new AgeComparator());
Collections.sort(list, personComparator); //人按照年龄进行排序
System.out.println(list);
personComparator.setComparator(new IDReverseComparator());
Collections.sort(list, personComparator); //人按照ID进行反向排序
System.out.println(list);
personComparator.setComparator(new NameReverseComparator());
Collections.sort(list, personComparator); //人按照Name进行反向排序
System.out.println(list);
personComparator.setComparator(new AgeReverseComparator());
Collections.sort(list, personComparator); //人按照ID进行反向排序
System.out.println(list);
}}
class Person {
String name;
public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}public String getId() {
return id;
}public void setId(String id) {
this.id = id;
}public int getAge() {
return age;
}public void setAge(int age) {
this.age = age;
}String id;
int age;public Person(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public String toString(){
return id +"-" +name+"-" + age;
}
}/**
*
* 抽象策略类 ,比较两个Person
*
* @author Administrator
*
*/
class PersonComparator implements Comparator{
Comparator comparator;
public Comparator getComparator() {
return comparator;
}
public void setComparator(Comparator comparator) {
this.comparator = comparator;
}
public PersonComparator(Comparator comparator){
this.comparator = comparator;
}
@Override
public int compare(Object o1, Object o2) {
return this.comparator.compare(o1,o2);
}
}/**
*
* 具体比较策略实现类, 用于比较两个ID,大的id返回1 ,小的返回-1
*
* @author Administrator
*
*/
class IDComparator implements Comparator {@Override
public int compare(Object o1, Object o2) {
Person person = (Person) o1 ;
Person person1 = (Person) o2;
int a = Integer.parseInt(person.getId()); //id 转换为数字,然后进行比较
int b = Integer.parseInt(person1.getId());
return a==b?0 : a>b?1:-1 ;
}
}/**
* 具体比较策略实现类 ,用于比较两个姓名,大的姓名放回1 ,小的返回-1 , 如果姓名一样的话,按照ID进行比较
* @author Administrator
*
*/class NameComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Person person = (Person) o1 ;
Person person1 = (Person) o2;
if(person.getName().equals(person1.getName())){
return new PersonComparator(new IDComparator()).compare(o1, o2); //姓名一样,用ID比较
}
return person.getName().compareTo(person1.getName());
}
}/**
* 具体比较策略实现类 ,用于比较两个年龄,大的年龄放回1 ,小的返回-1 , 如果年龄一样的话,按照ID进行比较
*
* @author Administrator
*
*/class AgeComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Person person = (Person) o1 ;
Person person1 = (Person) o2;
if(person.getAge()==person1.getAge()){
return new PersonComparator(new IDComparator()).compare(o1, o2); //年龄意一样,用ID比较
}
return person.getAge()>person1.getAge()?1:-1;
}
}
class AgeReverseComparator implements Comparator {@Override
public int compare(Object o1, Object o2) {
Person person = (Person) o1 ;
Person person1 = (Person) o2;
return - new AgeComparator().compare(o1, o2);
}
}
class NameReverseComparator implements Comparator {@Override
public int compare(Object o1, Object o2) {
Person person = (Person) o1 ;
Person person1 = (Person) o2;
return - new NameComparator().compare(o1, o2);
}
}class IDReverseComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Person person = (Person) o1 ;
Person person1 = (Person) o2;
return - new IDComparator().compare(o1, o2);
}
}