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));
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章