设计模式-java

设计模式之策略模式(行为型)--- 22

2019-07-24  本文已影响0人  auzqy
  • 一、导语
  • 二、怎么用
    1.样例背景
    2.UML类图
    3.代码示例
  • 三、优缺点
  • 四、使用场景
    1.概括描述
    2.现存知名产品中的使用示例
  • 五、与其他设计模式的对比
  • 六、参考

一、导语

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

策略模式是一种定义了一系列算法的方法,从概念上来看,所有这些算法完成的都是相同的工作,只是实现不同,他可以以相同的方式调用所有的算法,减少了各种算法类与使用算法类之间的耦合。

二、怎么用

共有1个示例,代码详见访问链接
下面以example1举例说明

1. 样例背景

商场促销,商场搞活动,有三种促销方式:
1.返现促销
2.满减促销
3.立减促销

example1/eg1_2 结合工厂模式实现一下上述效果
好处/目的:
1.消除if/else的判断
2.避免多次创建重复的对象(比如满减啊,返现啊等这些类)

2. UML类图

PromotionActivity --------------------- 促销活动类
IPromotionStrategy ------------------- 促销策略接口
PromotionStrategyFactory ------------ 生产具体促销策略的工厂类
DefaultPromotionStrategy ------------ 默认的促销策略
FanXianPromotionStrategy ----------- 返现促销策略
LiJianPromotionStrategy ------------- 立减促销策略
ManJianPromotionStrategy ----------- 满减促销策略

example1/eg1_2 使用策略模式+工厂模式后 UML类图

3. 代码示例

/**
 * @Description: 促销活动
 */
public class PromotionActivity {

    private IPromotionStrategy iPromotionStrategy;

    public PromotionActivity(IPromotionStrategy iPromotionStrategy) {
        this.iPromotionStrategy = iPromotionStrategy;
    }

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


/**
 * @Description: 促销策略
 */
public interface IPromotionStrategy {
    void doPromotion();
}


/**
 * @Description: 促销策略的工厂类
 */
public class PromotionStrategyFactory {

    // 维护促销策略的 Map
    private static Map<String, IPromotionStrategy> PROMOTION_STRATEGY_MAP = new HashMap<>();

    static {
        PROMOTION_STRATEGY_MAP.put(PromotionKey.FAN_XIAN,new FanXianPromotionStrategy());
        PROMOTION_STRATEGY_MAP.put(PromotionKey.LIN_JIAN,new LiJianPromotionStrategy());
        PROMOTION_STRATEGY_MAP.put(PromotionKey.MAN_JIAN,new ManJianPromotionStrategy());
    }

    public interface PromotionKey{
        String FAN_XIAN = "FAN_XIAN";
        String LIN_JIAN = "LIN_JIAN";
        String MAN_JIAN = "MAN_JIAN";
    }

    // 默认的促销策略
    private static final IPromotionStrategy NON_PROMOTION = new DefaultPromotionStrategy();

    public static IPromotionStrategy getPromotionStrategy(String promotionKey){
        IPromotionStrategy iPromotionStrategy = PROMOTION_STRATEGY_MAP.get(promotionKey);
        return null != iPromotionStrategy ? iPromotionStrategy : NON_PROMOTION;
    }
}


/**
 * @Description: 默认的促销策略
 */
public class DefaultPromotionStrategy implements IPromotionStrategy {
    @Override
    public void doPromotion() {
        System.out.println("默认的促销策略");
    }
}


/**
 * @Description: 返现策略
 */
public class FanXianPromotionStrategy implements IPromotionStrategy {
    @Override
    public void doPromotion() {
        System.out.println("执行返现策略");
    }
}


/**
 * @Description: 立减策略
 */
public class LiJianPromotionStrategy implements IPromotionStrategy {
    @Override
    public void doPromotion() {
        System.out.println("执行立减策略");
    }
}


/**
 * @Description: 满减策略
 */
public class ManJianPromotionStrategy implements IPromotionStrategy {
    @Override
    public void doPromotion() {
        System.out.println("执行满减策略");
    }
}


/**
 * @Description: 测试类
 * @Author: zqy
 * @CreateTime: 2019-07-24 15:07
 */
public class Test {

    public static void main(String[] args) {
        factoryStrategyUseCase();
    }

    /**
     * @Description: 使用 工厂方法 + 策略模式的情况
     *          好处:
     *              1。 在客户端消除了 if/else 的判断
     *              2。 避免多次创建重复的对象
     */
    public static void factoryStrategyUseCase(){
        String promotionKey = PromotionStrategyFactory.PromotionKey.FAN_XIAN;
        PromotionActivity promotionActivity = new PromotionActivity(
                PromotionStrategyFactory.getPromotionStrategy(promotionKey));
        promotionActivity.executePromotionStrategy();
    }

}

三、优缺点

四、使用场景

1. 概括描述

2. 现存知名产品中的使用示例 todo

2.1 java.util.Comparator (jdk)

2.2 org.springframework.core.io.Resource (spring)

2.3 org.springframework.beans.factory.support.InstantiationStrategy (spring)

五、相关设计模式

1. 策略模式和工厂模式

这里的工厂模式就包含抽象工厂和工厂方法;
抽象工厂是创建型的设计模式;
策略模式是是行为型的设计模式,也就是说工厂模式接受指令,创建出符合要求的具体对象,而策略模式,它接受已经创建好的对象,从而实现不同的行为。

2. 策略模式和状态模式

在使用策略模式时,我们的客户端,需要知道我们到底选择那个策略;
而我们在使用状态模式的时候,客户端是不需要关心具体的状态的,这些状态可以自动的转换。
如果系统中某个类的对象存在多种状态,那在不同状态下,他们的行为又有差异的话,而且这些状态可以发生转换时,我们可以使用状态模式;但如果系统中某些类的某个行为,存在多种实现方式,比如一个商城做618和双十一的促销活动,里面就有不同的策略,对于促销就是一种行为,而这一种行为有多种实现方式,这种情况下,就要使用策略模式。

六、参考

  1. https://coding.imooc.com/learn/list/270.html(强烈推荐)
  2. https://en.wikipedia.org/wiki/Design_Patterns
  3. 大话设计模式,电子版下载链接,https://pan.baidu.com/s/17WOI3Bvp-JUoQXvaomHISg 密码:vw05
    (作者博客,https://www.cnblogs.com/cj723/archive/2007/12/30/1021314.html)
  4. https://www.cnblogs.com/geek6/p/3951677.html
  5. https://mp.weixin.qq.com/s/eAlPRqScG3-Acvi3HwYK3A
上一篇 下一篇

猜你喜欢

热点阅读