策略模式

定义与类型

  • 定义:定义了算法家族,分别封装起来,让他们之间可以相互替换,此模式让算法的变化不会影响到使用算法的用户
  • if…else…
  • 类型:行为型
  • 适用场景
    • 系统有很多类,而他们的行为仅仅在于他们的行为不同
    • 一个系统需要动态地在几种算法中选择一种
  • 优点
    • 开闭原则
    • 避免使用多重条件转移语句
    • 提高算法的保密性和安全性
  • 缺点
    • 客户端必须知道所有的策略类,并自行决定使用哪一个策略类
    • 产生很多策略类(打个比方,原来我们把不同的策略封装到不同的方法里,里面通过大量的if’ else进行判断。这些都写到一个类里边,如果我们拆出来,一个类一个行为,一个类一个策略,那么就会产生很多策略实现类)
  • 策略模式-相关设计模式
    • 策略模式和工厂模式(这个工厂模式就包含工厂方法和抽象工厂,工厂方法是创建型的设计模式,而策略模式是行为型的设计模式。也就是说工厂模式接收指令,创建出符合要求的具体对象,而策略模式则接受已经创建好的对象,从而实现不同的行为)
    • 策略模式和状态模式(使用策略模式的时候,我们的客户端也就是Test类需要知道我们到底使用哪一个策略,而我们在使用状态模式的时候,客户端是不需要关心具体的状态的,这个状态会自动转换。总结来看就是如果系统中某个类的对象存在多种状态,在不同状态下行为又有差异的话,我们可以使用状态模式。如果系统中某个类的某个行为存在多种实现方式,例如慕课网做618和双11的促销,对于促销是一种行为,而这种行为有多种实现方式,这种情况下我们就要使用策略模式)

coding demo

public interface PromotionStrategy {
    void doPromotion();
}

public class EmptyPromotionStrategy implements PromotionStrategy {
    public void doPromotion() {
        System.out.println("无促销活动");
    }
}

public class FanXianPromotionStrategy implements PromotionStrategy {
    public void doPromotion() {
        System.out.println("返减促销");
    }
}

public class LiJianPromotionStrategy implements PromotionStrategy {
    public void doPromotion() {
        System.out.println("立减促销");
    }
}

public class ManJianPromotionStrategy implements PromotionStrategy {
    public void doPromotion() {
        System.out.println("满减促销");
    }
}

public class PromotionActivity {
    private PromotionStrategy promotionStrategy;

    public  PromotionActivity(PromotionStrategy promotionStrategy){
        this.promotionStrategy = promotionStrategy;
    }

    public void executePromotionStrategy(){
        promotionStrategy.doPromotion();
    }
}

public class PromotionStrategyFactory {
    private static Map<String,PromotionStrategy> PROMOTION_STRATEGY_MAP = new HashMap<String, PromotionStrategy>();

    private static final PromotionStrategy NON_PROMOTION = new EmptyPromotionStrategy();

    static {
        PROMOTION_STRATEGY_MAP.put(PromotionKey.LIJIAN,new LiJianPromotionStrategy());
        PROMOTION_STRATEGY_MAP.put(PromotionKey.MANJIAN,new ManJianPromotionStrategy());
        PROMOTION_STRATEGY_MAP.put(PromotionKey.FANXIAN,new FanXianPromotionStrategy());
    }

    private PromotionStrategyFactory(){}

    public static PromotionStrategy getPromotionStrategy(String promotion_key){
        PromotionStrategy promotionStrategy = PROMOTION_STRATEGY_MAP.get(promotion_key);
        return promotionStrategy == null ? NON_PROMOTION : promotionStrategy;
    }

    private interface PromotionKey{
        String LIJIAN = "LIJIAN";
        String FANXIAN = "FANXIAN";
        String MANJIAN = "MANJIAN";
    }
}

public class Test {
    public static void main(String[] args) {
        //V1
//        PromotionActivity promotionStrategy618 = new PromotionActivity(new ManJianPromotionStrategy());
//        PromotionActivity promotionActivity1111 = new PromotionActivity(new LiJianPromotionStrategy());
//
//        promotionStrategy618.executePromotionStrategy();
//        promotionActivity1111.executePromotionStrategy();
//
//

        //V2
        //if... else

        //V3
        String promotionKey = "MANJIAN";
        PromotionStrategy promotionStrategy = PromotionStrategyFactory.getPromotionStrategy(promotionKey);
        promotionStrategy.doPromotion();
    }

在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值