设计模式-策略模式

基于比较器的策略模式

 

优点:使用策略模式可以加强工程扩展性,维护性。避免过多的if...else..或者switch,隐藏了策略实现的细节。

缺点:相对于if ..else 不能直观看到所有策略方式,并且每增加一种策略都要新加一个策略类。

通过以下比较猫狗大小的例子,来了解策略模式。

代码结构

 

实体类dto

public class Cat {
    public int weight;
    public int height;
    public String name;

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

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

    public Dog(int weight, int height, String name) {
        this.weight = weight;
        this.height = height;
        this.name = name;
    }

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

比较接口:用于不同策略实现比较方法

public interface Comparator<T> {
     int copmare(T o1, T o2);
}

不同维度比较猫狗大小的策略

1、根据猫的高比较大小

public class CatHighCompare implements Comparator<Cat> {

    @Override
    public int copmare(Cat o1, Cat o2) {
        if(o1.height > o2.height) return -1;
        else if (o1.height < o2.height) return 1;
        else return 0;
    }
}

2、根据猫的体重比较大小

public class CatWeightCompare implements Comparator<Cat> {

    @Override
    public int copmare(Cat o1, Cat o2) {
        if(o1.weight > o2.weight) return -1;
        else if (o1.weight < o2.weight) return 1;
        else return 0;
    }
}

3、根据狗的高比较大小

public class DogHighCompare implements Comparator<Dog> {
    @Override
    public int copmare(Dog o1, Dog o2) {
        if(o1.height > o2.height) return -1;
        else if (o1.height < o2.height) return 1;
        else return 0;
    }
}

4、根据狗的体重比较大小

public class DogWeightCompare implements Comparator<Dog> {
    @Override
    public int copmare(Dog o1, Dog o2) {
        if(o1.weight > o2.weight) return -1;
        else if (o1.weight < o2.weight) return 1;
        else return 0;
    }
}

当然如果需要其他维度,可以继续加策略类就行

排序Sorter类

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++) {
                int copmare = comparator.copmare(arr[j], arr[minPos]);
                minPos = copmare == -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;
    }
}

测试类

public class Test {
    public static void main(String[] args) {
        Cat[] cats = {new Cat(5,1,"汤姆猫"),new Cat(2,2,"吉利猫"),new Cat(6,3,"熊仔猫")};
        Sorter<Cat> catSorter = new Sorter<>();//猫类型的sort
        catSorter.sort(cats, new CatWeightCompare());//调用sort使用不同的比较策略
        for (int i = 0; i < cats.length; i++) {
            System.out.println("按照猫体重排序:"+ cats[i]);
        }

        System.out.println();

        catSorter.sort(cats, new CatHighCompare());//调用sort使用不同的比较策略

        for (int i = 0; i < cats.length; i++) {
            System.out.println("按照猫身高排序:"+ cats[i]);
        }

        System.out.println();

        Sorter<Dog> dogSorter = new Sorter<>();//狗类型的sort
        Dog[] dogs = {new Dog(10,12,"汤姆狗"),new Dog(8,6,"吉利狗"),new Dog(16,22,"熊仔狗")};

        dogSorter.sort(dogs, new DogWeightCompare());//调用sort使用不同的比较策略

        for (int i = 0; i < cats.length; i++) {
            System.out.println("按照狗身高排序:"+ cats[i]);
        }
        System.out.println();
        dogSorter.sort(dogs, new DogHighCompare());//调用sort使用不同的比较策略
        for (int i = 0; i < cats.length; i++) {
            System.out.println("按照狗体重排序:"+ cats[i]);
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值