跳转至

策略模式

1. 介绍

  在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改。这种类型的设计模式属于行为型模式。在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 context 对象。策略对象改变 context 对象的执行算法。

2. 示例

Comparator.class 接口
package com.cmz.strategy;

/**
 * @author summer
 * @create 2020-03-05 14:08
 */

@FunctionalInterface
public interface Comparator<T> {

    int compare(T o1,T o2);
}
比较器 Sorter.class
package com.cmz.strategy;

/**
 * @author summer
 * @create 2020-03-05 13:09
 *
 * https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/Comparable.html
 */
public class Sorter<T> {
    public void sort(T[] arr, Comparator<T> comparator) {
        for(int i=0; i<arr.length - 1; 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;
    }
}

Cat.class
package com.cmz.strategy;

/**
 * @author summer
 * @create 2020-03-05 13:15
 */
public class Cat implements Comparable<Cat>{
    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 +
                '}';
    }
}
策略1 CatHeightComparator.class
package com.cmz.strategy;

/**
 * @author summer
 * @create 2020-03-05 14:14
 */
public class CatHeightComparator implements Comparator<Cat> {
    @Override
    public int compare(Cat o1, Cat o2) {
        if(o1.height > o2.height) return -1;
        else if(o1.height < o2.height) return 1;
        else return 0;
    }
}
策略2 CatWeightComparator.class
package com.cmz.strategy;
public class CatWeightComparator implements Comparator<Cat> {
    @Override
    public int compare(Cat o1, Cat o2) {
        if(o1.weight < o2.weight) return -1;
        else if(o1.weight > o2.weight) return 1;
        else return 0;
    }
}

Dog.class
package com.cmz.strategy;

/**
 * @author summer
 * @create 2020-03-05 13:38
 */
public class Dog implements Comparable<Dog> {
    int food;

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

    @Override
    public int compareTo(Dog d) {

        if(this.food < d.food) return -1;
        else if (this.food > d.food) return 1;
        else return 0;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "food=" + food +
                '}';
    }
}
策略1 DogComparator.class
package com.cmz.strategy;

/**
 * @author summer
 * @create 2020-03-05 14:14
 */
public class DogComparator implements Comparator<Dog> {
    @Override
    public int compare(Dog o1, Dog o2) {
        if(o1.food < o2.food) return -1;
        else if(o1.food > o2.food) return 1;
        else return 0;
    }
}

Main.class
package com.cmz.strategy;

import java.util.Arrays;

/**
 * @author summer
 * @create 2020-03-05 13:07
 */
public class Main {
    public static void main(String[] args) {
//        int[] a = {9,2,3,5,7,1,4};
//        Cat[] a= {new Cat(3,3),new Cat(5,5),new Cat(1,1)};
        Dog[] dogs= {new Dog(6),new Dog(8),new Dog(10)};
        Sorter<Dog> sorter_dogs = new Sorter();
        sorter_dogs.sort(dogs,new DogComparator());
        System.out.println(Arrays.toString(dogs));

        // 根据weight
        Cat[] cats_weight = {new Cat(3,3),new Cat(5,5),new Cat(1,1)};
        Sorter<Cat> sorter_cats_weight = new Sorter();
        sorter_cats_weight.sort(cats_weight,new CatWeightComparator());
        System.out.println(Arrays.toString(cats_weight));

        // 根据height
        Cat[] cats_height = {new Cat(3,3),new Cat(5,5),new Cat(1,1)};
        Sorter<Cat> sorter_cat_cats_heights = new Sorter();
        sorter_cat_cats_heights.sort(cats_height,new CatHeightComparator());
        System.out.println(Arrays.toString(cats_height));

        // lambda
        Cat[] cats_height1 = {new Cat(3,3),new Cat(5,5),new Cat(1,1)};
        Sorter<Cat> cats_height_lambda = new Sorter();
        cats_height_lambda.sort(cats_height1,(o1,o2)->{
            if(o1.height < o2.height) return -1;
            else if (o1.height > o2.height) return 1;
            else return 0;
        });
        System.out.println(Arrays.toString(cats_height1));

    }
}
运行结果
[Dog{food=6}, Dog{food=8}, Dog{food=10}]
[Cat{weight=1, height=1}, Cat{weight=3, height=3}, Cat{weight=5, height=5}]
[Cat{weight=5, height=5}, Cat{weight=3, height=3}, Cat{weight=1, height=1}]
[Cat{weight=1, height=1}, Cat{weight=3, height=3}, Cat{weight=5, height=5}]