码迷,mamicode.com
首页 > 其他好文 > 详细

策略模式

时间:2014-05-03 22:07:51      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   color   

 现在,我们要对对象实例进行比较。既然是对象比较,那么比较的规则不尽相同。比较规则不同,那么我们就可以对不同的东西设置一个接口,在这里也就是将比较规则放到一个Comparable的接口中,实现其中的compareTo方法就得到给出具体比较规则的结果。这也就意味着,要进行比较的对象,都得首先实现这个接口。

bubuko.com,布布扣
  1 package program.strategy.model;
  2 
  3 public interface Comparable {
  4     public int compareTo(Object o);
  5 }
  6 
  7 
  8 
  9 public class Cat implements Comparable{
 10 
 11     private int id;
 12     private int weight;
 13     
 14     public Cat(int id,int weight) {
 15         this.id=id;
 16         this.weight=weight;
 17     }
 18     @Override
 19     public int compareTo(Object o) {
 20        if(o instanceof Cat) {
 21            Cat c=(Cat)o;
 22            if(this.weight>c.weight) {
 23                return 1;
 24            }else if(this.weight<c.weight) {
 25                return -1;
 26            }
 27            return 0;
 28        }
 29         throw new RuntimeException("对象类型不正确");
 30     }
 31     
 32     public String toString() {
 33         return "cat"+id+"_weight"+weight;
 34     }
 35     public int getId() {
 36         return id;
 37     }
 38     public void setId(int id) {
 39         this.id = id;
 40     }
 41     public int getWeight() {
 42         return weight;
 43     }
 44     public void setWeight(int weight) {
 45         this.weight = weight;
 46     }
 47     
 48 
 49 
 50 public class Dog implements Comparable{
 51     private int id;
 52     private int weight;
 53     public Dog(int id,int weight) {
 54         this.id=id;
 55         this.weight=weight;
 56     }
 57     
 58     @Override
 59     public int compareTo(Object o) {
 60         if(o instanceof Dog) {
 61             Dog d=(Dog)o;
 62             if(this.weight>d.weight) {
 63                 return 1;
 64             }else if(this.weight<d.weight) {
 65                 return -1;
 66             }
 67             return 0;
 68         }
 69         throw new RuntimeException("类型比较错误");
 70     }
 71     
 72     public String toString() {
 73         return "dog"+id+"_weight"+weight;
 74     }
 75 
 76     public int getId() {
 77         return id;
 78     }
 79 
 80     public void setId(int id) {
 81         this.id = id;
 82     }
 83 
 84     public int getWeight() {
 85         return weight;
 86     }
 87 
 88     public void setWeight(int weight) {
 89         this.weight = weight;
 90     }
 91 }
 92 
 93 
 94 
 95 public class DataSorter {
 96     public static void sort(Object[] objs) {
 97         int len=objs.length;
 98         for(int i=len-1;i>0;i--) {
 99             Comparable o1=(Comparable)objs[i];
100             for(int j=0;j<i;j++) {
101                 Comparable o2=(Comparable)objs[j];
102                 if(o1.compareTo(o2)<0) {
103                     Object o=objs[j];
104                     objs[j]=objs[i];
105                     objs[i]=o;
106                 }
107             }
108         }
109     }
110 }
111 
112 
113 
114 public class Main {
115     public static void main(String[]args) {
116         Dog[] dogs=new Dog[]{new Dog(1,2),new Dog(2,34),new Dog(3,4)};
117         DataSorter.sort(dogs);
118         for(Dog d:dogs) {
119             System.out.println(d);
120         }
121     }
122 }
bubuko.com,布布扣

评价:上面的类,就DataSorter就相当于是类库提供的,它提供了排序的算法。我们只要给对象提供比较的规则,当然就可以对对象进行排序。但是,除了DataSorter,其他所有的类对比较的实体类都存在依赖性,这也就意味着,如果改变比较的规则,要修改的地方是挺多的。上面的接口只是提供了一种提供比较规则的方法,现在,我们可以对于可能将来会发生变化的比较规则再提供一个接口Comparator。

bubuko.com,布布扣
 1 package program.strategy.model;
 2 
 3 public interface Comparator {
 4     public int compare(Object o1,Object o2);
 5 }
 6 
 7 
 8 
 9 public class WeightComparator implements Comparator{
10     @Override
11     public int compare(Object o1, Object o2) {
12         Cat c1=(Cat)o1;
13         Cat c2=(Cat)o2;
14         if(c1.getWeight()>c2.getWeight()) {
15             return 1;
16         }else if(c1.getWeight()<c2.getWeight()) {
17             return -1;
18         }
19         return 0;
20     }
21 
22 }
23 
24 
25 
26 public class Cat implements Comparable{
27     private int id;
28     private int weight;
29     private Comparator comparator=new WeightComparator();
30     
31     public Cat(int id,int weight) {
32         this.id=id;
33         this.weight=weight;
34     }
35     @Override
36     public int compareTo(Object o) {
37        return comparator.compare(this, o);
38     }
39     
40     public String toString() {
41         return "cat"+id+"_weight"+weight;
42     }
43     public int getId() {
44         return id;
45     }
46     public void setId(int id) {
47         this.id = id;
48     }
49     public int getWeight() {
50         return weight;
51     }
52     public void setWeight(int weight) {
53         this.weight = weight;
54     }
55 }
bubuko.com,布布扣

这样,通过添加一个比较规则类WeightComparator从而降低了各个类与具体比较规则的耦合度。因为,它将这种关联都放到了WeightComparator当中;如果我们以后要对身高进行比较,就可以添加比较规则类HeightComparator即可。另外,该具体类当中比较规则的实例化。

策略模式,布布扣,bubuko.com

策略模式

标签:style   blog   class   code   java   color   

原文地址:http://www.cnblogs.com/feijishuo/p/3705344.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!