设计模式--策略模式--简记

策略模式(Strategy Pattern):

  一个类的行为或算法可以在运行是更改。

  比如:要实现一个通用的比较大小的类方法,单个对象比较大小的策略可能会发生改变,可能会出现多种比较策略,我们可定义一个比较策略接口,对象类中比较的大小的方法,可以通过更改策略实现对象,来进行替换。

 

优点:

  算法可以自由切换、避免多重条件判断、扩展性好

缺点:

  策略类会增多,策略类需要对外暴露

例:定义一个猫类

 1 package cn.qf.lean.dp;
 2 
 3 import java.util.Comparator;
 4 
 5 /*
 6  * 定义一只猫类,类中含比较猫的大小的方法
 7  * 猫大小的比较含多个维度的比较策略
 8  */
 9 
10 public class Cat implements Comparable<Cat> {
11     
12     private int height;
13     private int weight;
14     //通过高度的比较策略比较猫的大小
15     private Comparator<Cat> comparator = new CatHeightComparator();
16     //通过宽度的比较策略比较猫的大小
17 //    private Comparator<Cat> comparatorW = new CatWeightComparator();
18         
19     public Cat(int i, int j) {
20         this.height = i;
21         this.weight = j;
22     }
23     public int getHeight() {
24         return height;
25     }
26     public void setHeight(int height) {
27         this.height = height;
28     }
29     public int getWeight() {
30         return weight;
31     }
32     public void setWeight(int weight) {
33         this.weight = weight;
34     }
35     
36     public Comparator<Cat> getComparator() {
37         return comparator;
38     }
39     public void setComparator(Comparator<Cat> comparator) {
40         this.comparator = comparator;
41     }
42     
43     @Override
44     public int compareTo(Cat o) {
45         return comparator.compare(this, o);
46     }
47     
48     @Override
49     public String toString() {
50         return height + "|" + weight;
51     }
52 
53 }
View Code

猫大小比较的两种策略

 1 package cn.qf.lean.dp;
 2 
 3 import java.util.Comparator;
 4 
 5 /*
 6  * 通过比较猫的高度的策略,比较猫的大小
 7  */
 8 
 9 public class CatHeightComparator implements Comparator<Cat> {
10 
11     
12 
13     @Override
14     public int compare(Cat o1, Cat o2) {
15         if(o1.getHeight() > o2.getHeight()) return 1;
16         else if(o1.getHeight() < o2.getHeight()) return -1;
17         else return 0;
18     }
19 
20 }
View Code
 1 package cn.qf.lean.dp;
 2 
 3 import java.util.Comparator;
 4 
 5 /*
 6  * 通过比较猫的宽度的策略,比较猫的大小
 7  */
 8 
 9 public class CatWeightComparator implements Comparator<Cat> {
10 
11 
12     @Override
13     public int compare(Cat o1, Cat o2) {
14         if(o1.getWeight() > o2.getWeight()) return 1;
15         else if(o1.getWeight() < o2.getWeight()) return -1;
16         else return 0;
17     }
18 
19 }
View Code

比较类

 1 package cn.qf.lean.dp;
 2 
 3 /*
 4  * 比较算法
 5  */
 6 
 7 public class DataSorter {
 8     
 9     public static void sort(Comparable[] a) {
10         
11         for(int i=a.length; i>0; i--) {
12             for(int j=0; j<i-1; j++) {
13                 Comparable c1 = (Comparable)a[j];
14                 Comparable c2 = (Comparable)a[j+1];
15                 if(c1.compareTo(c2) == 1) {
16                     swap(a, j, j+1);
17                 }
18             }
19         }
20     }
21     
22     
23     private static void swap(Object[] a, int x, int y) {
24         // TODO Auto-generated method stub
25         Object temp = a[x];
26         a[x] = a[y];
27         a[y] = temp;
28     }
29 
30 
31 
32     public static void p(Object[] a) {
33         
34         for(int i=0; i<a.length; i++) {
35             System.out.print(a[i] + " ");
36         }
37         System.out.print("\n");
38     }
39 
40 }
View Code

测试

package cn.qf.lean.dp;

public class Test {
    
    public static void main(String[] args) {
        
        Cat[] a = {new Cat(5, 5), new Cat(3, 3), new Cat(1,1)};
        DataSorter.sort(a);
        DataSorter.p(a);
        
    }
}

 

posted on 2018-03-18 09:14  我要的明天  阅读(161)  评论(0编辑  收藏  举报

导航