设计模式:策略模式

    策略模式 Strategy pattern

   我们一个功能的实现,存在很多种算法,很多种方式,比如数据库,我们可以有很多种是数据库进行选择,我们的mq,也有很多种,我们的zk客户端,也存在很多种,所在为了系统的稳定性,和可扩展性,我们要把大多数情况都要考虑到位,免得后期增加的时候,不易实现。

  传统的方式,我们可以给一个标识符来代替我们使用的哪种方案,然后我们通过if-else-if-else来进行判断,这种情况下,代码就显得十分的繁重,

  策略模式,使用聚合把这些方案都封装起来,(封装它的接口,而不是使用继承),这样给用户的感觉就不会存在多种情况。

一,策略模式

方案顶层接口

public interface PromotionStrategy {
    public void doPromotion();//促销活动
}

三种实现的方案:

public class CouponStrategy implements PromotionStrategy {
    @Override
    public void doPromotion() {
        System.out.println("领取优惠券,抵扣");
    }
}
public class CashbackStrategy implements PromotionStrategy {
    @Override
    public void doPromotion() {
        System.out.println("返现促销,返回的金额到支付宝");
    }
}
public class GroupbuyStrategy implements PromotionStrategy {
    @Override
    public void doPromotion() {
        System.out.println("无促销活动");
    }
}

策略模式,对三种方案进行聚合:

public class PromotionActivity {
    private PromotionStrategy promotionStrategy;
    public PromotionActivity(PromotionStrategy promotionStrategy){
        this.promotionStrategy=promotionStrategy;
    }
    public void execute(){
        promotionStrategy.doPromotion();
    }
}

业务测试:

public static void main(String[] args) {
        PromotionActivity activity=null;
        String promotionKey="COUPON"; //用户触发的方式
        if(promotionKey.equals("COUPON"))
           activity=new PromotionActivity(new CouponStrategy());
        else if(promotionKey.equals("CASHBACK"))
           activity=new PromotionActivity(new CashbackStrategy());
        else if(promotionKey.equals("Groupbuy"))
            activity=new PromotionActivity(new GroupbuyStrategy());
        activity.execute();
    }

结果:

二,策略的改进

使用工厂模式+单例模式,对if-else进行优化

//工厂模式
public class PromotionStrategyFactory {
    private static Map<String,PromotionStrategy> map=new HashMap<>();
    static {
        map.put(PromotionKey.COUPON,new CouponStrategy());
        map.put(PromotionKey.CASHBACK,new CashbackStrategy());
        map.put(PromotionKey.GROUPBUY,new GroupbuyStrategy());
    }
    private PromotionStrategyFactory(){} //单例模式
    public static PromotionStrategy getPromotionStrategy(String promotionKey){
        PromotionStrategy promotionStrategy=map.get(promotionKey);
        return promotionStrategy;
    }
    
    private interface PromotionKey{
        String COUPON="COUPON";
        String CASHBACK="CASHBACK";
        String GROUPBUY="GOUPBUY";
    }
}

测试:

 public static void main(String[] args) {
        PromotionActivity activity=null;
       String key="GOUPBUY";
       activity=new PromotionActivity(PromotionStrategyFactory.getPromotionStrategy(key));
        activity.execute();
    }

结果:

 

三,总结

优化:

  1.策略模式提高算法的保密性,安全性,

缺点:

 1.客户端需要知道所有的策略,手动选择策略

 2.代码会产生非常多的策略类,增加代码的维护难度。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值