java中排序实现简单的策略模式

jopen 13年前
     <b> <span style="font-size:18px;">策略模式(Strategy Pattern)中体现了两个非常基本的面向对象设计的原则</span></b>    <div>     <b><span style="font-size:18px;">–封装变化的概念</span></b>    </div>    <div>     <b><span style="font-size:18px;">–编程中使用接口,而不是对接口的实现</span></b>    </div>    <div>     <b><br /> </b>    </div>    <div>     <div>      <b><span style="font-size:18px;">策略模式的定义 </span></b>     </div>     <div>      <b><span style="font-size:18px;">–定义一组算法,将每个算法都封装起来,并且使它们之间可以互换。 </span></b>     </div>     <div>      <b><span style="font-size:18px;">–策略模式使这些算法在客户端调用它们的时候能够互不影响地变化 </span></b>     </div>    </div>    <div>     <b><br /> </b>    </div>    <div>     <div>      <b><span style="font-size:18px;">策略模式的编写步骤 </span></b>     </div>     <div>      <b><span style="font-size:18px;">–1.对策略对象定义一个公共接口。 </span></b>     </div>     <div>      <b><span style="font-size:18px;">–2.编写策略类,该类实现了上面的公共接口 </span></b>     </div>     <div>      <b><span style="font-size:18px;">–3.在使用策略对象的类中保存一个对策略对 </span></b>     </div>     <div>      <b><span style="font-size:18px;">象的引用。 </span></b>     </div>     <div>      <b><span style="font-size:18px;">–4.在使用策略对象的类中,实现对策略对象 </span></b>     </div>     <div>      <b><span style="font-size:18px;">的set和get方法(注入)或者使用构造方法完 </span></b>     </div>     <p><b><span style="font-size:18px;">成赋值</span></b></p>     <p><b><span style="font-size:18px;"><br /> </span></b></p>     <p><b><span style="font-size:18px;">java排序策略模式实现:</span></b></p>     <p><span style="font-size:medium;"><span style="line-height:27px;"><b>有这样一个类:</b></span></span></p>     <p></p>     <p><b>public class Person {</b></p>     <b> <p>private int id;</p> <p>private String name;</p> <p>private int age;</p> <p>}</p> <p></p> <p>要求:假如有若干个类Person对象存在一个List当中,对他们进行排序,分别按照名字、</p> <p></p> <b> <p>年龄、id 进行排序(要有正序与倒序两种排序方式)。假如年龄或者姓名重复,按照id的正序进行排序。</p> </b><b> <p></p> </b> <p></p> <p></p> <p><b>以下为实现源代码。</b></p> <p></p> <p></p> <p><b>类Person</b></p> <p></p> <p></p> </b>     <p></p>     <p></p>     <pre class="brush:java; toolbar: true; auto-links: false;"><b>package com.jack.SortStrategy;  public class Person {  private int id;  private String name;  private int age;    public Person(int id,String name,int age){   this.id=id;   this.age=age;   this.name=name;  }  /**   * @return the id   */  public int getId() {   return id;  }  /**   * @param id the id to set   */  public void setId(int id) {   this.id = id;  }  /**   * @return the name   */  public String getName() {   return name;  }  /**   * @param name the name to set   */  public void setName(String name) {   this.name = name;  }  /**   * @return the age   */  public int getAge() {   return age;  }  /**   * @param age the age to set   */  public void setAge(int age) {   this.age = age;  } }</b></pre>     <p></p>     <p><b><span style="font-size:18px;">类IdOrder</span></b></p>     <p></p>     <pre class="brush:java; toolbar: true; auto-links: false;"><b>package com.jack.SortStrategy;  import java.util.Comparator;  public class IdOrder implements Comparator<Person> {   @Override  public int compare(Person o1, Person o2) {   // TODO Auto-generated method stub   return o1.getId()-o2.getId();  }  public String getMethodName()  {   return "按id正序";  }  }</b></pre>     <p></p>     <p><b><span style="font-size:18px;">类IdReverseOrder</span></b></p>     <p></p>     <pre class="brush:java; toolbar: true; auto-links: false;"><b>package com.jack.SortStrategy;  import java.util.Comparator;  public class IdReverseOrder implements Comparator<Person> {    @Override  public int compare(Person o1, Person o2) {   // TODO Auto-generated method stub   return o2.getId()-o1.getId();  }  public String getMethodName()  {   return "按id逆序";  } }</b></pre>     <p></p>     <p><b><span style="font-size:18px;">类AgeOrder</span></b></p>     <p></p>     <pre class="brush:java; toolbar: true; auto-links: false;"><b>package com.jack.SortStrategy;  import java.util.Comparator;  public class AgeOrder implements Comparator<Person> {    @Override  public int compare(Person o1, Person o2) {   // TODO Auto-generated method stub   int result=o1.getAge()-(o2.getAge());   if(0==result){                                      //若年龄相同按id排    return o1.getId()-o2.getId();   }   return result;  }  public String getMethodName()  {   return "按年龄正序";  } }</b></pre>     <p></p>     <p><b><span style="font-size:18px;">类AgeReverseOrder</span></b></p>     <p></p>     <pre class="brush:java; toolbar: true; auto-links: false;"><b>package com.jack.SortStrategy;  import java.util.Comparator;  public class AgeReverseOrder implements Comparator<Person> {    @Override  public int compare(Person o1, Person o2) {   // TODO Auto-generated method stub   int result=o2.getAge()-(o1.getAge());   if(0==result){                                      //若年龄相同按id排    return o1.getId()-o2.getId();   }   return result;  }  public String getMethodName()  {   return "按年龄逆序";  } }</b></pre>     <p></p>     <p><b><span style="font-size:18px;">类</span></b><span style="font-size:medium;"><span style="line-height:27px;"><b>NameOrder</b></span></span></p>     <p></p>     <pre class="brush:java; toolbar: true; auto-links: false;"><b>package com.jack.SortStrategy;  import java.util.Comparator;  public class NameOrder implements Comparator<Person> {   @Override  public int compare(Person o1, Person o2) {   // TODO Auto-generated method stub   int result=o1.getName().compareTo(o2.getName());   if(0==result){                                                              //若姓名相同按id排    return o1.getId()-o2.getId();   }   return result;  }  public String getMethodName()  {   return "按姓名正序";  } }</b></pre>     <p></p>     <p><span style="font-size:medium;"><span style="line-height:27px;"><b>类NameReverseOrder</b></span></span></p>     <p></p>     <pre class="brush:java; toolbar: true; auto-links: false;"><b>package com.jack.SortStrategy;  import java.util.Comparator;  public class NameReverseOrder implements Comparator<Person> {  @Override  public int compare(Person o1, Person o2) {   // TODO Auto-generated method stub   int result=o2.getName().compareTo(o1.getName());   if(0==result){                                                              //若姓名相同按id排    return o1.getId()-o2.getId();   }   return result;  }  public String getMethodName()  {   return "按姓名逆序";  } }</b></pre>     <p></p>     <p><span style="font-size:medium;"><span style="line-height:27px;"><b>类Client</b></span></span></p>     <p></p>     <pre class="brush:java; toolbar: true; auto-links: false;"><b>package com.jack.SortStrategy;  import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Iterator;  public class Client {   /**   * @param args   */  public static void main(String[] args) {   // TODO Auto-generated method stub   ArrayList<Person> personList = new ArrayList<Person>();   personList.add(new Person(1, "zhangsan", 20));   personList.add(new Person(2, "lisi", 21));   personList.add(new Person(3, "wangwu", 25));   personList.add(new Person(4, "zhaoliu", 26));   personList.add(new Person(5, "chenqi", 28));   personList.add(new Person(6, "dongba", 10));   personList.add(new Person(7, "zhansan", 15));   personList.add(new Person(8, "liujiu", 26));       /*    * 可通过这里new不同的Comparator的实现从而实现不同的排序策略    * 分别有    *  IdOrder:按Id正序    *  IdReverseOrder:按Id逆序    * NameOrder:按姓名正序    * NameReverseOrder:按姓名逆序    * AgeOrder:按年龄正序    * AgeReverseOrder:按年龄逆序    */   Comparator<Person> comp = new IdOrder();                            Collections.sort(personList, comp);    System.out.println(((IdOrder)comp).getMethodName()+"  排序后的序列是:");      System.out.println("-----------------------");    for (Iterator<Person> itor=personList.iterator();itor.hasNext();) {        Person p=itor.next();    System.out.println(p.getId() + "  " +p.getName()+ "  " + p.getAge());   }   }  }</b></pre>     <div>      <b><br /> </b>     </div>     <p></p>     <b>执行Client可以得到排序的结果</b>     <p></p>     <p></p>     <p></p>     <p></p>     <p></p>     <p></p>     <p></p>     <p></p>    </div>