Java设计模式之_Strategy(策略)_DesignPattern

  由于此前看了马老师的这节视频,所以就在此粗粗的总结一下,也是隔了好长时间,肯定也是阐述的不是很好了。言归正传,开始今天晚上的总结。

  这一节是什么呢?就是传说中的策略模式,我记得开始研究模式的时候是从看李刚老师的《轻量级javaEE》上开始研究,但是也没有研究很明白,可能是对java的核心思想都没有理解到位的缘故吧。哈哈。。。

Strategy----策略模式

  由于讲的案例是关于排序的,所以就要亮出这个口诀【冒择路(入)兮(希尔)快归堆】

  言归正传,开始study。。。。。。。。。。。。。。。。

  Step1 – 我理解的Strategy就是对对象的属性的操作的封装。归结到根本还是java的封装、继承和多态的三大特点。

本课的主要讲的呢,就是猫和狗的故事。

他们的什么故事呢?

猫猫怎么比较大小,狗狗又怎么比较大小?

  Step2 – Strategy引入

猫猫是对象有一大堆的属性,不像一些基础的数据类型,可以直接比较,那到底该怎么比较呢?

当然这种抽象的比较方法最终还是要归结到基础数据类型之间的比较,那么对象又怎么来“简化”?

其实就是将这些对象都统一通过他们的一种或多种属性进行比较,那么这些参与比较的属性又该怎么封装呢?

很简单,既然要封装肯定要在类里,那么没个对象也都有了,干脆专门声明一个方法来对用于他们之间的比较,那么这些对象就可以通过这个方法来比较了。

我认为这就是一种策略吧~~~

  Step3 – 深入Strategy(刚学设计模式,当然就要多用了,不怕麻烦,就怕不用)

上述的那种的方法只解决了那个类的对象的比较问题,如果好多的类的对象也需要比较呢?而且在通过sort方法进行比较时,他的内部是不是用的对象的相同方法来进行比较呢?(毕竟这样的话就能少写好多代码,也很严谨)

public interface Comparable {
    public int compareTo(Object o);
}
------------------------------------
public interface Comparator {
    int compare(Object o1, Object o2);
}
--------------------------
public class Dog implements Comparable {
    public Dog(int food) {
        super();
        this.food = food;
    }

    private int food;

    public int getFood() {
        return food;
    }

    public void setFood(int food) {
        this.food = food;
    }

    @Override
    public int compareTo(Object o) {
        Dog d = (Dog)o;
        if(this.food > d.getFood()) return 1;
        else if(this.food < d.getFood()) return -1;
        else return 0;
    }
    
    @Override
    public String toString() {
        return this.food + "";
    }
}
---------------------------
public class CatHeightComparator implements java.util.Comparator<Cat> {

    @Override
    public int compare(Cat o1, Cat o2) {
        Cat c1 = (Cat)o1;
        Cat c2 = (Cat)o2;
        if(c1.getHeight() > c2.getHeight()) return 1;
        else if(c1.getHeight() < c2.getHeight()) return -1;
        return 0;
        
    }

}
---------------------
public class CatWeightComparator implements Comparator {

    @Override
    public int compare(Object o1, Object o2) {
        Cat c1 = (Cat)o1;
        Cat c2 = (Cat)o2;
        if(c1.getWeight() > c2.getWeight()) return -1;
        else if(c1.getHeight() < c2.getHeight()) return 1;
        return 0;
    }

}
-------------------------
public class Cat implements java.lang.Comparable<Cat> {
    private int height;
    //private Comparator comparator = new CatHeightComparator();
    private java.util.Comparator<Cat> comparator = new CatHeightComparator();
    public java.util.Comparator getComparator() {
        return comparator;
    }
    public void setComparator(java.util.Comparator comparator) {
        this.comparator = comparator;
    }
    public int getHeight() {
        return height;
    }
    public void setHeight(int height) {
        this.height = height;
    }

    
    public Cat(int height, int weight) {
        super();
        this.height = height;
        this.weight = weight;
    }
    public int getWeight() {
        return weight;
    }
    public void setWeight(int weight) {
        this.weight = weight;
    }
    private int weight;
    
    @Override
    public String toString() {
        return height + "|" + weight;
    }
    @Override
    public int compareTo(Cat o) {
        return comparator.compare(this, o);
    }
}
-------------------------------
public class DataSorter {

    public static void sort(Object[] a) {
        for(int i=a.length; i>0; i--) {
            for(int j=0; j<i-1; j++) {
                Comparable o1 = (Comparable)a[j];
                Comparable o2 = (Comparable)a[j+1];
                if(o1.compareTo(o2) == 1) {
                    swap(a, j , j+1);
                }
            }
        }
    }
    
    
    
    private static void swap(Object[] a, int x, int y) {
        Object temp = a[x];
        a[x] = a[y];
        a[y] = temp;
    }

    public static void sort(int[] a) {
        for(int i=a.length; i>0; i--) {
            for(int j=0; j<i-1; j++) {
                if(a[j] > a[j+1]) {
                    swap(a, j , j+1);
                }
            }
        }
    }

    private static void swap(int[] a, int x, int y) {
        int temp = a[x];
        a[x] = a[y];
        a[y] = temp;
    }

    public static void p(int[] a) {
        for(int i=0; i<a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }

    public static void p(Object[] a) {
        for(int i=0; i<a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }

}
------------------------------
public class Test {
    public static void main(String[] args) {
        //int[] a = {9, 5, 3, 7, 1};
        Cat[] a = {new Cat(5, 5), new Cat(3, 3), new Cat(1, 1)};
        //Dog[] a = {new Dog(5), new Dog(3), new Dog(1)};
        //DataSorter.sort(a);
        java.util.Arrays.sort(a);
        DataSorter.p(a);        
    }
}

  这种问题的解决方案,就是让他们都是实现同一个接口。另外使用比较器作为策略也是很好的一种策略,具体看你要解决什么样的问题了!!!

这两种比较策略在上面的例子里都应用了,不过【java.lang.Comparable<Cat>】是完全可以替换成【Comparable】也就是自己写的比较接口,而不是sun写的。

原文地址:https://www.cnblogs.com/wang-s/p/4631932.html