Spring高级特性-多种类会员折扣问题

解决问题:当有多种类型的会员时,订单计费的价格是有差异的,代码中太多的if和else必然导致代码的臃肿和难以维护,其实可以优雅的设计它。

1、定以订单类

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    //自动注入一个map(key是托管给Spring管理的引用名称,value为真实对象)
    @Autowired
    private Map<String, CalculateAmountService> calculateAmountServiceMap;

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public double getPaymentAmount(String userType, double amount) {
        log.debug("所有的折扣对象:{}", calculateAmountServiceMap);
        //根据用户的类型从map对象中找到对应的对象进行金额的计算
        CalculateAmountService service = calculateAmountServiceMap.get(userType);
        if (Objects.isNull(service)) {
            return amount;
        }
        return service.getPaymentAmountByDisCount(amount);
    }
}

2、定义不同用户类型计算的基类

package com.milla.navicat.spring.study.service;

/**
 * @Package: com.milla.navicat.spring.study.service
 * @Description: <根据不同的折扣计算不同的金额>
 * @Author: MILLA
 * @CreateDate: 2019/11/1 16:58
 * @UpdateUser: MILLA
 * @UpdateDate: 2019/11/1 16:58
 * @UpdateRemark: <>
 * @Version: 1.0
 */
public interface CalculateAmountService {
    //根据用户的类型打折
    double getPaymentAmountByDisCount(double fee);
}

3、具体实现类

3.1、普通会员实现类

package com.milla.navicat.spring.study.service.impl;

import com.milla.navicat.spring.study.service.CalculateAmountService;
import org.springframework.stereotype.Service;

/**
 * @Package: com.milla.navicat.spring.study.service.impl
 * @Description: <会员折扣>
 * @Author: MILLA
 * @CreateDate: 2019/11/1 16:59
 * @UpdateUser: MILLA
 * @UpdateDate: 2019/11/1 16:59
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@Service(value = "normal")//托管处理时的引用名称
public class NormalCalculateAmountServiceImpl implements CalculateAmountService {

    @Override
    public double getPaymentAmountByDisCount(double fee) {
        return fee * 0.9;
    }
}

3.2、VIP会员实现类

package com.milla.navicat.spring.study.service.impl;

import com.milla.navicat.spring.study.service.CalculateAmountService;
import org.springframework.stereotype.Service;

/**
 * @Package: com.milla.navicat.spring.study.service.impl
 * @Description: <会员折扣>
 * @Author: MILLA
 * @CreateDate: 2019/11/1 16:59
 * @UpdateUser: MILLA
 * @UpdateDate: 2019/11/1 16:59
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@Service(value = "vip")
public class VIPCalculateAmountServiceImpl implements CalculateAmountService {

    @Override
    public double getPaymentAmountByDisCount(double fee) {
        return fee * 0.8;
    }
}

3.3、金卡会员实现类

package com.milla.navicat.spring.study.service.impl;

import com.milla.navicat.spring.study.service.CalculateAmountService;
import org.springframework.stereotype.Service;

/**
 * @Package: com.milla.navicat.spring.study.service.impl
 * @Description: <至尊会员折扣>
 * @Author: MILLA
 * @CreateDate: 2019/11/1 16:59
 * @UpdateUser: MILLA
 * @UpdateDate: 2019/11/1 16:59
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@Service(value = "gold")
public class GoldCalculateAmountServiceImpl implements CalculateAmountService {

    @Override
    public double getPaymentAmountByDisCount(double fee) {
        return fee * 0.55;
    }
}

3.4、超级会员实现类

package com.milla.navicat.spring.study.service.impl;

import com.milla.navicat.spring.study.service.CalculateAmountService;
import org.springframework.stereotype.Service;

/**
 * @Package: com.milla.navicat.spring.study.service.impl
 * @Description: <超级会员折扣>
 * @Author: MILLA
 * @CreateDate: 2019/11/1 16:59
 * @UpdateUser: MILLA
 * @UpdateDate: 2019/11/1 16:59
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@Service(value = "svip")
public class SVIPCalculateAmountServiceImpl implements CalculateAmountService {

    @Override
    public double getPaymentAmountByDisCount(double fee) {
        return fee * 0.5;
    }
}

4、测试类

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class MyNavicatApplicationTests { 
@Test
    public void testDiscountByUserType() {
        log.info("youke  类型的折扣:{}", saleService.getPaymentAmount("youke", 100));
        log.info("normal 类型的折扣:{}", saleService.getPaymentAmount("normal", 100));
        log.info("vip 类型的折扣:{}", saleService.getPaymentAmount("vip", 100));
        log.info("svip 类型的折扣:{}", saleService.getPaymentAmount("svip", 100));
        log.info("gold 类型的折扣:{}", saleService.getPaymentAmount("gold", 100));
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值