1    代码演练

1.1  代码演练1(多态)

1.2  代码演练2(优化应用)

1.3  代码演练3(策略模式结合工厂模式)

 

 

 

1    代码演练

 

1.1  代码演练1(多态)

需求:

木木网卖课程,为了促进销售,618实行买课程立减10元,双十一实行满50减10元,还有返现的优惠(这个活动还没有开始)。请实现它

 

uml类图:

 

测试类:

 

package com.geely.design.pattern.behavioral.strategy;

public class TestStrategy {
    public static void main(String [] args){
        PromotionActivity promotionActivityLJ = new PromotionActivity(new LiJianPromotionStrategy());
        PromotionActivity promotionActivityMJ = new PromotionActivity(new ManJianPromotionStrategy());

        promotionActivityLJ.execute();
        promotionActivityMJ.execute();
    }
}

 

 

 

应用层:

package com.geely.design.pattern.behavioral.strategy;

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

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

 

 

 

实现类1:

package com.geely.design.pattern.behavioral.strategy;

public class LiJianPromotionStrategy implements PromotionStrategy {
    @Override
    public void doPromotion() {
        System.out.println("立减10元");
    }
}

 

 

 

实现类2:

package com.geely.design.pattern.behavioral.strategy;

public class ManJianPromotionStrategy implements PromotionStrategy {
    @Override
    public void doPromotion() {
        System.out.println("满50元减10元");
    }
}

 

 

 

实现类3:

package com.geely.design.pattern.behavioral.strategy;

public class FanXianPromotionStrategy implements PromotionStrategy {
    @Override
    public void doPromotion() {
        System.out.println("返现10元到木木网");
    }
}

 

 

 

接口:

package com.geely.design.pattern.behavioral.strategy;

/**
 * 初始接口
 */
public interface PromotionStrategy {
    void doPromotion();
}

 

打印日志:

立减10元
满50元减10元

Process finished with exit code 0

 

 

1.2  代码演练2(优化应用类)

测试类优化:

package com.geely.design.pattern.behavioral.strategy;

import org.apache.commons.lang.StringUtils;

public class TestStrategy {

    public static void main(String [] args){
        PromotionActivity promotionActivity = null;
        String promotionKey = "ManJian";

        if(StringUtils.equals(promotionKey,"LiJian")){
            promotionActivity = new PromotionActivity(new LiJianPromotionStrategy());
        }else if(StringUtils.equals(promotionKey,"ManJian")){
            promotionActivity = new PromotionActivity(new ManJianPromotionStrategy());
        }
        promotionActivity.execute();
    }
}

 

 

 

 

1.3  代码演练3(策略模式结合工厂模式)

目的:

a  将参数设置成可以配置的,简化以后维护的成本

b  取消大量的if...else...应用

 

 

测试类:

 

package com.geely.design.pattern.behavioral.strategy;

import org.apache.commons.lang.StringUtils;

/**
 * add by ddwei 20200213
 */
public class TestStrategy {
    //demo 1  常规多态实现
   /* public static void main(String [] args){
        PromotionActivity promotionActivity618 = new PromotionActivity(new FanXianPromotionStrategy());
        PromotionActivity promotionActivity1111 = new PromotionActivity(new LiJianPromotionStrategy());
        promotionActivity1111.executePromotion();
        promotionActivity618.executePromotion();
    }*/


    //demo 2  正常程序不可避免 的使用if else
    /*public static void main(String [] args){
        String promotionStrategy = "LIJIAN";
        PromotionActivity promotionActivity = null;
        if(StringUtils.equals(promotionStrategy,"LIJIAN")){
            promotionActivity = new PromotionActivity(new LiJianPromotionStrategy());
        }else if(StringUtils.equals(promotionStrategy,"FANXIAN")){
            promotionActivity = new PromotionActivity(new FanXianPromotionStrategy());
        } //...
        promotionActivity.executePromotion();
    }*/


    //demo 3  结合工厂模式 ,策略模式推进 ,不再使用if else
    public static void main(String [] args){
        //String promotionStrategy = "LIJIAN";
        //String promotionStrategy = "FANXIAN";
        String promotionStrategy = "LIJIAN3";
        PromotionStrategy iPromotionStrategy = PromotionFactory.getPromotionStrategy(promotionStrategy);
        PromotionActivity promotionActivity = new PromotionActivity(iPromotionStrategy);
        promotionActivity.executePromotion();
    }

}

 

 

 

核心类(策略模式结合工厂模式):

 

package com.geely.design.pattern.behavioral.strategy;

import java.util.HashMap;
import java.util.Map;

/**
 * 策略工厂
 */
public class PromotionFactory {

    private static Map<String,PromotionStrategy> promotionStrategyHashMap= new HashMap<String, PromotionStrategy>();
    //静态代码块 HashMap放值
    static{
        promotionStrategyHashMap.put(IPromotionConfigtation.MANJIAN,new ManJianPromotionStrategy());
        promotionStrategyHashMap.put(IPromotionConfigtation.LIJIAN,new LiJianPromotionStrategy());
        promotionStrategyHashMap.put(IPromotionConfigtation.FANXIAN,new FanXianPromotionStrategy());
    }

    /**
     * 主方法
     * 根据传入的策略key值 查询到相应的策略.
     * 如果未查到相应的key值返回空策略,目的是避免出现空指针异常的情况
     * 从根本上代替了if...else 的功能
     * @param promotionKey
     * @return
     */
    public static PromotionStrategy getPromotionStrategy(String promotionKey){
        PromotionStrategy EMPTYPROMOTIONSTRATEGY = new EmptyPromotionStrategy();
        return promotionStrategyHashMap.containsKey(promotionKey)?promotionStrategyHashMap.get(promotionKey):EMPTYPROMOTIONSTRATEGY;
    }

    /**
     * 后期维护入口:
     * 内部类,放入配置参数
     */
    interface IPromotionConfigtation{
        String MANJIAN = "MANJIAN";
        String LIJIAN = "LIJIAN";
        String FANXIAN = "FANXIAN";
    }


    //禁止外部调用构造函数来创建对象
    public PromotionFactory() {
    }
}

 

 

 

促销活动类:

 

package com.geely.design.pattern.behavioral.strategy;

/**
 * 促销应用类
 * add by ddwei 20200213
 */
public class PromotionActivity {
    //有参构造传值
    private PromotionStrategy promotionStrategy;

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

    //调用执行策略方法
    public void executePromotion(){
        promotionStrategy.doPromotion();
    }

}

 

 

 

子类空策略类:

 

package com.geely.design.pattern.behavioral.strategy;

/**
 * 空策略
 */
public class EmptyPromotionStrategy implements PromotionStrategy {
    @Override
    public void doPromotion() {
        System.out.println("todo 无相应的优惠!");
    }
}

 

 

 

子类满减类:

 

package com.geely.design.pattern.behavioral.strategy;

public class ManJianPromotionStrategy implements PromotionStrategy {
    @Override
    public void doPromotion() {
        System.out.println("todo 实现满减逻辑");
    }
}

 

 

 

子类立减类:

 

package com.geely.design.pattern.behavioral.strategy;

public class LiJianPromotionStrategy implements PromotionStrategy {
    @Override
    public void doPromotion() {
        System.out.println("todo 实现立减逻辑");
    }
}

 

 

 

子类返现类:

 

package com.geely.design.pattern.behavioral.strategy;

public class FanXianPromotionStrategy implements PromotionStrategy {
    @Override
    public void doPromotion() {
        System.out.println("todo 实现返现逻辑");
    }
}

 

 

 

父类策略接口:

 

package com.geely.design.pattern.behavioral.strategy;

/**
 * 促销通用接口
 * add by ddwei 20200213
 */
public interface PromotionStrategy {
    void doPromotion();
}

 

 

 

打印结果:

todo 无相应的优惠!

Process finished with exit code 0

 

posted on 2019-10-09 07:01  菜鸟乙  阅读(197)  评论(0编辑  收藏  举报