设计模式——策略模式

2019-07-18  本文已影响0人  prik丶

《Head First 设计模式》 学习笔记
码云同步更新中
如有错误或不足之处,请一定指出,谢谢~

目录

查看其它设计模式笔记,点这里→设计模式笔记汇总

策略模式

/**
 * 策略接口
 *
 * @author Xushiwei
 * @date 2019/7/18
 */
public interface DiscountStrategy {

    /**
     * 计算折扣后价格
     *
     * @param originalPrice 原价
     * @return 折扣后应付价格
     */
    BigDecimal calDiscountPrice(BigDecimal originalPrice);
}

/**
 * 普通客户策略实现
 *
 * @author Xushiwei
 * @date 2019/7/18
 */
public class OrdinaryDiscountStrategy implements DiscountStrategy {
    @Override
    public BigDecimal calDiscountPrice(BigDecimal originalPrice) {
        System.out.println("普通客户,无优惠");
        return originalPrice;
    }
}

/**
 * vip客户策略实现,九折
 *
 * @author Xushiwei
 * @date 2019/7/18
 */
public class VipDiscountStrategy implements DiscountStrategy {
    @Override
    public BigDecimal calDiscountPrice(BigDecimal originalPrice) {
        System.out.println("VIP客户,九折优惠~");
        return originalPrice.multiply(new BigDecimal("0.9"));
    }
}

/**
 * 顾客
 * 注:@Data为lombok注解
 *
 * @author Xushiwei
 * @date 2019/7/18
 */
@Data
public class Customer {

    /**
     * 姓名
     */
    private String name;

    /**
     * 客户类型
     */
    private Integer type;

    /**
     * 普通客户
     */
    public static final int TYPE_ORDINARY = 1;
    /**
     * vip客户
     */
    public static final int TYPE_VIP = 2;
    /**
     * 合作伙伴
     */
    public static final int TYPE_PARTNER = 3;
}

/**
 * 策略工厂
 * 单例
 *
 * @author Xushiwei
 * @date 2019/7/18
 */
public class DiscountStrategyFactory {

    /**
     * private修饰的构造函数,防止使用者手动创建
     */
    private DiscountStrategyFactory() {
    }

    private static DiscountStrategyFactory factory = new DiscountStrategyFactory();

    private static Map<Integer, DiscountStrategy> strategyMap = new HashMap();

    static{
        strategyMap.put(Customer.TYPE_ORDINARY, new OrdinaryDiscountStrategy());
        strategyMap.put(Customer.TYPE_VIP, new VipDiscountStrategy());
        strategyMap.put(Customer.TYPE_PARTNER, new PartnerDiscountStrategy());
    }

    public DiscountStrategy creator(int customerType) {
        return strategyMap.get(customerType);
    }

    public static DiscountStrategyFactory getInstance() {
        return factory;
    }
}

/**
 * 上下文类
 *
 * @author Xushiwei
 * @date 2019/7/18
 */
public class DiscountContext {
    private static DiscountStrategyFactory strategyFactory = DiscountStrategyFactory.getInstance();
    
    public BigDecimal calDiscountPrice(BigDecimal originalPrice, int customerType) {
        DiscountStrategy strategy = strategyFactory.creator(customerType);
        return strategy.calDiscountPrice(originalPrice);
    }
}

/**
 * 测试类
 *
 * @author Xushiwei
 * @date 2019/7/18
 */
public class Test {
    public static void main(String[] args) {
        BigDecimal originalPrice = new BigDecimal(100);

        // 普通客户
        DiscountContext discountContext = new DiscountContext();
        BigDecimal price1 = discountContext.calDiscountPrice(originalPrice, 1);

        System.out.println("普通客户下单,原价:" + originalPrice + "元,应付款:" + price1 + "元");

        // vip客户
        BigDecimal price2 = discountContext.calDiscountPrice(originalPrice, 2);
        System.out.println("vip客户下单,原价:" + originalPrice + "元,应付款:" + price2 + "元");
    }
}

执行结果:
    普通客户,无优惠
    普通客户下单,原价:100元,应付款:100元
    VIP客户,九折优惠~
    vip客户下单,原价:100元,应付款:90.0元
上一篇下一篇

猜你喜欢

热点阅读