java设计模式之策略模式(strategy)

一、首先我们要了解到策略模式中它有四种角色:

1、抽象策略,也就是策略接口类或者策略抽象类。

2、具体策略,就是策略实现类。

3、策略执行者。

4、策略使用者。

二、接下来看看下面四个小案例:

1、对一组人的身高进行排序。

抽象策略

package com.liuyc.designpattern.strategy;

public class PersonHeightSortStrategy implements SortStrategy<Person>{

    @Override
    public int compareTo(Person t1, Person t2) {
        if(t1.getHeight()>t2.getHeight()){
            return 1;
        }else {
            return 0;
        }
    }
}

具体策略类

package com.liuyc.designpattern.strategy;

public class PersonHeightSortStrategy implements SortStrategy<Person>{

    @Override
    public int compareTo(Person t1, Person t2) {
        if(t1.getHeight()>t2.getHeight()){
            return 1;
        }else {
            return 0;
        }
    }
}

策略执行者

package com.liuyc.designpattern.strategy;

public class SortHelper<T> {

    public void sort(T [] t ,SortStrategy<T> sortStrategy){
        //选择排序法
        //第一个循环,控制比较次数
        for (int i = 0 ; i < t.length -1 ; i ++ ){
            //第2个循环,从左往右找出最大的数字
            int tempMaxIndex = i;
            for (int j = i+1 ;j < t.length ; j ++){
                tempMaxIndex = sortStrategy.compareTo(t[j],t[tempMaxIndex]) > 0 ? j:tempMaxIndex;
            }
            //每一次找出最大数字放到已排好序的数字最右边
            T temp = t[i];
            t[i] = t[tempMaxIndex];
            t[tempMaxIndex] = temp;
        }
    }
}

策略使用者

        Person person1 = new Person(100, 500);
        Person person2 = new Person(200, 400);
        Person person3 = new Person(300, 200);
        Person [] peoples = new Person[]{person1,person2,person3};

        SortHelper<Person> sortHelper2 = new SortHelper<Person>();
        //2、使用按身高排序策略(从高到矮)
        sortHelper2.sort(peoples,new PersonHeightSortStrategy());
        for (int i = 0; i < peoples.length; i++) {
            System.out.println(peoples[i]);
        }

person类

package com.liuyc.designpattern.strategy;

public class Person {
    /**
     * 身高
     */
    private int height;

    /**
     * 体重
     */
    private int weight;

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

    public int getHeight() {
        return height;
    }

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

    public int getWeight() {
        return weight;
    }

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

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

2、对一组人的体重进行排序,只需要提供具体策略即可。

具体策略类

package com.liuyc.designpattern.strategy;

public class PersonWeightSortStrategy implements SortStrategy<Person>{

    @Override
    public int compareTo(Person t1, Person t2) {
        if(t1.getWeight()>t2.getWeight()){
            return 1;
        }else {
            return 0;
        }
    }
}

策略使用者

        Person person1 = new Person(100, 500);
        Person person2 = new Person(200, 400);
        Person person3 = new Person(300, 200);
        Person [] peoples = new Person[]{person1,person2,person3};        
        //3、使用按体重排序策略(重到轻)
        sortHelper2.sort(peoples,new PersonWeightSortStrategy());
        for (int i = 0; i < peoples.length; i++) {
            System.out.println(peoples[i]);
        }

3、对一个double数据进行排序,从大到小,只需要提供具体策略即可。

具体策略类

package com.liuyc.designpattern.strategy;

public class DoubleArraySortStrategy implements SortStrategy<Double>{
    @Override
    public int compareTo(Double t1, Double t2) {
        if(t1 > t2){
            return 1;
        }else{
            return -1;
        }
    }
}

策略使用者

        //4、给double数组排序
        Double [] doubles = {31d,11d,25d,91d,43d,10d,5d};

        SortHelper<Double> sortHelper3 = new SortHelper<Double>();
        sortHelper3.sort(doubles,new DoubleArraySortStrategy());

        for (int i = 0; i < doubles.length; i++) {
            System.out.print(doubles[i] +"  ");
        }

有问题欢迎留言,谢谢!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
策略模式Strategy Pattern)是Java设计模式中的一种行为型模式,它定义了一系列的算法,并将每个算法封装在独立的类中,使得它们可以互相替换。这样可以使得算法的变化独立于使用它们的客户端。 在策略模式中,有三个主要角色: 1. 环境类(Context):持有一个策略类的引用,用于调用具体的策略。 2. 抽象策略类(Strategy):定义了一个公共接口或抽象类,用于具体策略类的统一调用。 3. 具体策略类(Concrete Strategy):实现了抽象策略类定义的接口或抽象类,提供具体的算法实现。 使用策模式可以实现算法的动态切换,增加新的算法也不会影响到已有的代码。例如,假设我们需要实现一个排序算法,可以定义一个抽象策略类 SortStrategy,然后具体的排序算法(如快速排序、归并排序等)分别实现 SortStrategy,并在环境类中持有 SortStrategy 的引用。这样,通过更换不同的 SortStrategy 对象,就可以在运行时选择不同的排序算法。 策略模式能够有效地解耦策略的定义和使用,提高代码的灵活性和可维护性。同时,它也符合面向对象设计原则中的"开闭原则"(对扩展开放,对修改关闭)和"单一职责原则"(一个类应该只有一个引起变化的原因)。 希望这个简要的介绍能够帮助到你对策略模式的理解。如果还有其他问题,可以继续提问!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值