Java设计模式10: 策略模式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_37591536/article/details/83116522

一、什么是策略模式?

策略模式属于对象的行为模式。其用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换。策略模式使得算法可以在不影响到客户端的情况下发生变化。

策略模式把一个系列的算法封装到一个系列的具体策略类里面,作为一个抽象策略类的子类或策略接口的实现类。简单地说:准备一组算法,并将每一个算法封装起来,使它们可以互换。

这个模式涉及到3个角色:

  环境(Context)角色:持有一个Strategy抽象策略类或策略接口的引用。

  抽象策略(Strategy)角色:这是一个抽象角色,由一个接口或抽象类实现。此角色声明所有的具体策略类需要重写的方法。

  具体策略(Concrete Strategy)角色:封装了相关的算法或行为。

二、直接上代码示例:

这样一个场景:某线上超市初级会员不打折,中级会员打8折,高级会员打5折

先创建一个策略模式接口:


/**
 * <pre>
 *    @author : orange
 *    @e-mail : [email protected]
 *    @time   : 2018/10/17 18:12
 *    @desc   : 策略模式抽象接口
 *    @version: 1.0
 * </pre>
 */
public interface MemberStrategy {

    Double calculationPrice(Double price);
}

再分别创建策略接口的实现类:

初级会员实现类

/**
 * <pre>
 *    @author : orange
 *    @e-mail : [email protected]
 *    @time   : 2018/10/17 18:18
 *    @desc   :
 *    @version: 1.0
 * </pre>
 */
public class JuniorMemberStrategy implements MemberStrategy {
    @Override
    public Double calculationPrice(Double price) {
        System.out.println("初级会员不打折扣");
        return null;
    }
}

中级会员实现类

/**
 * <pre>
 *    @author : orange
 *    @e-mail : [email protected]
 *    @time   : 2018/10/17 18:44
 *    @desc   :
 *    @version: 1.0
 * </pre>
 */
public class IntermediateMemberStrategy implements MemberStrategy{


    @Override
    public Double calculationPrice(Double price) {

        System.out.println("中级会员打8折");

        return price * 0.8;
    }
}

高级会员实现类

/**
 * <pre>
 *    @author : orange
 *    @e-mail : [email protected]
 *    @time   : 2018/10/17 19:18
 *    @desc   :
 *    @version: 1.0
 * </pre>
 */
public class SeniorMemberStrategy implements MemberStrategy {
    @Override
    public Double calculationPrice(Double price) {
        System.out.println("高级会员打折腿。");
        return price * 0.5;
    }
}

价格类

/**
 * <pre>
 *    @author : orange
 *    @e-mail : [email protected]
 *    @time   : 2018/10/17 19:26
 *    @desc   :
 *    @version: 1.0
 * </pre>
 */
public class Price {

    private MemberStrategy memberStrategy;

    public Price(MemberStrategy memberStrategy){
        this.memberStrategy = memberStrategy;
    }

    public Double calPrice(Double price){

        return memberStrategy.calculationPrice(price);
    }


}

客户端测试

/**
 * <pre>
 *    @author : orange
 *    @e-mail : [email protected]
 *    @time   : 2018/10/17 19:29
 *    @desc   : 客户端测试
 *    @version: 1.0
 * </pre>
 */
public class Client {

    public static void main(String[] args) {
        SeniorMemberStrategy seniorMemberStrategy = new SeniorMemberStrategy();
        Price price = new Price(seniorMemberStrategy);
        Double calPrice = price.calPrice(100D);
        System.out.println("calPrice:" + calPrice);

    }

}

总结:

策略模式优点:

  1 通过策略类的等级结构来管理算法族。

  2 避免使用将采用哪个算法的选择与算法本身的实现混合在一起的多重条件(if-else if-else)语句。

  策略模式缺点:

  1 客户端必须知道所有的策略类,并自行决定使用哪一个策略类。

  2 由于策略模式把每个算法的具体实现都单独封装成类,针对不同的情况生成的对象就会变得很多。

猜你喜欢

转载自blog.csdn.net/weixin_37591536/article/details/83116522
今日推荐