美文网首页
23种设计模式-策略模式

23种设计模式-策略模式

作者: 大白乐了 | 来源:发表于2019-08-22 10:32 被阅读0次
  1. 需求: 有Dog和Cat类,对Dog[],Cat[]排序,满足可扩展性、灵活性、易维护性
  • v1: 在Cat类 Dog类定义方法compareTo
// Cat类
public class Cat {
    private int weight, height;

    public Cat(int weight, int height) {
        this.weight = weight;
        this.height = height;
    }

    public int compareTo(Cat c) {
        if (this.weight < c.weight) return -1;
        else if(this.weight > c.weight) return 1;
        else return 0;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "weight=" + weight +
                ", height=" + height +
                '}';
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}

// 排序类
public class SorterCat {

    /**
     * 思路: 循环数组,取出数组的每一个值,与大于这个值下标的值比较,取出最小值,
     * 最小值与当前的数组值对换
     * @param arr
     */
    public static void sort(Cat[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int minPos = i;

            for (int j = i + 1; j < arr.length; j++) {
                minPos = arr[j].compareTo(arr[minPos]) == -1 ? j : minPos;
            }

            swap(arr, i, minPos);
        }
    }

    // 交换数组不同下标的值
    static void swap(Cat[] arr, int i, int j) {
        Cat temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void main(String[] args) {
        Cat[] arr = {new Cat(3,3), new Cat(5, 5),new Cat(1, 1) };
        sort(arr);
        for (Cat item : arr) {
            System.out.println(item);
        }

    }
}
  • v2: v1中的compareTo可以抽象comparable接口
// cat类
public class CatC implements Comparable<CatC> {
    private int weight, height;

    public CatC(int weight, int height) {
        this.weight = weight;
        this.height = height;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "weight=" + weight +
                ", height=" + height +
                '}';
    }

    @Override
    public int compareTo(CatC c) {
        if (this.weight < c.weight) return -1;
        else if(this.weight > c.weight) return 1;
        else return 0;
    }
}

// dog类
public class DogC implements Comparable<DogC> {

    private int food;

    public DogC(int food) {
        this.food = food;
    }


    @Override
    public int compareTo(DogC d) {
        if (this.food < d.food) return -1;
        else if (this.food > d.food) return 1;
        else return 0;
    }

    @Override
    public String toString() {
        return "DogC{" +
                "food=" + food +
                '}';
    }
}

public class SorterComparable {

    /**
     * 思路: 循环数组,取出数组的每一个值,与大于这个值下标的值比较,取出最小值,
     * 最小值与当前的数组值对换
     * @param arr
     */
    public static void sort(Comparable[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int minPos = i;

            for (int j = i + 1; j < arr.length; j++) {
                minPos = arr[j].compareTo(arr[minPos]) == -1 ? j : minPos;
            }

            swap(arr, i, minPos);
        }
    }

    // 交换数组不同下标的值
    static void swap(Comparable[] arr, int i, int j) {
        Comparable temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}


//main
@Test
public void testComparable(){
      Comparable[] dogs = {new DogC(3),new DogC(2),new DogC(1)};
      SorterComparable.sort(dogs);
        for (Comparable dog : dogs) {
            System.out.println(dog);
        }

        System.out.println("========");

        Comparable[] cats = {new CatC(3,2),new CatC(2,1),new CatC(1,10)};
        SorterComparable.sort(cats);
        for (Comparable cat : cats) {
            System.out.println(cat);
        }
}



  • v3: 因为compareTo中的比较器,可能随时需要变化,v2不能满足这个需求,抽象出来Comparator接口,可以传递比较行为(由使用者指定),这样就灵活很多

/**
 * 比较器接口实现策略模式
 * @param <T>
 */
public interface Comparator<T> {

    int compare(T o1, T o2);

}

// Cat类
public class Cat {
    private int weight, height;

    public Cat(int weight, int height) {
        this.weight = weight;
        this.height = height;
    }


    @Override
    public String toString() {
        return "Cat{" +
                "weight=" + weight +
                ", height=" + height +
                '}';
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}

public class Sorter<T> {

    /**
     * 思路: 循环数组,取出数组的每一个值,与大于这个值下标的值比较,取出最小值,
     * 最小值与当前的数组值对换
     * @param arr
     */
    public void sort(T[] arr, Comparator<T> comparator) {
        for (int i = 0; i < arr.length; i++) {
            int minPos = i;

            for (int j = i + 1; j < arr.length; j++) {
                minPos = comparator.compare(arr[j], arr[minPos]) == -1 ? j : minPos;
            }

            swap(arr, i, minPos);
        }
    }

    // 交换数组不同下标的值
    void swap(T[] arr, int i, int j) {
        T temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

@Test
public viod testComparator(){
        Sorter<Cat> sorter = new Sorter<>();
        Cat[] arr = {new Cat(3,1), new Cat(2,9), new Cat(1,3)};
        Comparator<Cat> comparator = (o1, o2) -> {
            if (o1.getHeight() < o2.getHeight()) return -1;
            else if (o1.getHeight() > o2.getHeight()) return 1;
            else return 0;
        };
        Comparator<Cat> comparator1 = (o1, o2) -> {
            if (o1.getWeight() < o2.getWeight()) return -1;
            else if (o1.getWeight() > o2.getWeight()) return 1;
            else return 0;
        };
//      这里可随意切换比较器,实现了灵活扩展
        sorter.sort(arr,comparator);
//        sorter.sort(arr,comparator1);
        for (Cat c : arr) {
            System.out.println(c);
        }
}

相关文章

网友评论

      本文标题:23种设计模式-策略模式

      本文链接:https://www.haomeiwen.com/subject/xqigsctx.html