美文网首页
策略模式

策略模式

作者: KingdomCoder | 来源:发表于2019-01-02 22:48 被阅读0次

定义:

定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化不会影响到使用算法的用户。(行为型)

适用场景:

1.系统有很多类,而他们且别仅仅在于他们的行为不同。
2.一个系统需要动态的在几种算法中选择一种。

优点:

1.开闭原则。
2.避免使用多重条件转移语句。
3.提高算法的保密性和安全性。

缺点:

1.客户端必须知道所有的策略类,并自行决定使用哪一个策略类。
2.会产生很多策略类。

Coding:

声明策略方法:

/**
 * @program: design-pattern
 * @description: 促销策略类
 * @author: KingdomCoder
 * @create: 2019/1/2 下午10:17
 **/
public interface PromotionStrategy {

     void doPromotion();

}

返现策略

/**
 * @program: design-pattern
 * @description: 返现策略
 * @author: KingdomCoder
 * @create: 2019/1/2 下午10:18
 **/
public class FanxianPromotionStrategy implements PromotionStrategy {

    @Override
    public void doPromotion() {
        System.out.println("返现促销,返回的金额存放到账户中");
    }
}

立减策略:


/**
 * @program: design-pattern
 * @description: 立减策略
 * @author: KingdomCoder
 * @create: 2019/1/2 下午10:18
 **/
public class LijianPromotionStrategy implements PromotionStrategy{

    @Override
    public void doPromotion() {
        System.out.println("立减促销,产品价格减去减去的价格");
    }
}

满减促销:

/**
 * @program: design-pattern
 * @description: 满减促销
 * @author: KingdomCoder
 * @create: 2019/1/2 下午10:18
 **/
public class ManjianPromotionStrategy implements PromotionStrategy {

    @Override
    public void doPromotion() {
        System.out.println("满减促销满200-20");
    }
}

默认策略:

/**
 * @program: design-pattern
 * @description: 空策略作为默认策略
 * @author: KingdomCoder
 * @create: 2019/1/2 下午10:35
 **/
public class EmptyPromotionStrategy implements PromotionStrategy {

    @Override
    public void doPromotion() {
        System.out.println("无促销");
    }
}

活动类中执行不同的策略:

/**
 * @program: design-pattern
 * @description: 活动
 * @author: KingdomCoder
 * @create: 2019/1/2 下午10:22
 **/
@AllArgsConstructor
public class PromotionActivity {

    private  PromotionStrategy promotionStrategy;

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

}

测试类:

/**
 * @program: design-pattern
 * @description: 测试类
 * @author: KingdomCoder
 * @create: 2019/1/2 下午10:27
 **/
public class Test {

    public static void main(String[] args) {
        PromotionActivity promotionActivity618 = new PromotionActivity(new LijianPromotionStrategy());
        PromotionActivity promotionActivity1111 = new PromotionActivity(new ManjianPromotionStrategy());
        promotionActivity618.executePromotionStrategy();
        promotionActivity1111.executePromotionStrategy();
    }


}

测试结果:

立减促销,产品价格减去减去的价格
满减促销满200-20

Process finished with exit code 0

简单工厂+策略模式:

/**
 * @program: design-pattern
 * @description: 策略工厂
 * @author: KingdomCoder
 * @create: 2019/1/2 下午10:32
 **/
public class PromotionStrategyFactory {

    private static final Map<String, PromotionStrategy> PROMOTION_STRATEGY_MAP = new HashMap<>();

    private static final PromotionStrategy EMPTY_PROMOTION_STRATEGY = new EmptyPromotionStrategy();

    static {
        PROMOTION_STRATEGY_MAP.put(promotionStrategyKey.LIJIAN, new LijianPromotionStrategy());
        PROMOTION_STRATEGY_MAP.put(promotionStrategyKey.FANXIAN, new FanxianPromotionStrategy());
        PROMOTION_STRATEGY_MAP.put(promotionStrategyKey.MANJIAN, new ManjianPromotionStrategy());
    }

    private PromotionStrategyFactory() {

    }

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

    private interface promotionStrategyKey {

        String LIJIAN = "lijian";
        String FANXIAN = "fanxian";
        String MANJIAN = "manjian";
    }

}

测试:

/**
 * @program: design-pattern
 * @description: 测试
 * @author: KingdomCoder
 * @create: 2019/1/2 下午10:41
 **/
public class StrategyFactoryTest {

    public static void main(String[] args) {
        PromotionActivity promotionActivity618 = new PromotionActivity(PromotionStrategyFactory.getPromotionStrategy("manjian"));
        promotionActivity618.executePromotionStrategy();
    }

}

执行结果为:

满减促销满200-20

Process finished with exit code 0
微信公众号欢迎关注.jpg

相关文章

网友评论

      本文标题:策略模式

      本文链接:https://www.haomeiwen.com/subject/avotrqtx.html