解決問題:當有多種類型的會員時,訂單計費的價格是有差異的,代碼中太多的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));
}
}