优化if-else的几种方式
- 策略模式
- 1、创建支付策略接口
- 2、书写不同的支付方式逻辑代码
- 微信支付
- QQ支付
- 3、service层的实现类使用
- 4、controller层的调用
- 说明
- 枚举与策略模式结合
- 1、创建枚举
- 2、service层书写处理方法
- 3、controller层调用
- 4、说明
- Lambda表达式与函数接口
- 说明
策略模式
1、创建支付策略接口
2、书写不同的支付方式逻辑代码
微信支付
QQ支付
3、service层的实现类使用
import com.hysoft.study.service.PaymentStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* 支付实现类
*/
@Service
public class PaymentServiceImpl {
private final Map<String, PaymentStrategy> strategies;
@Autowired
public PaymentServiceImpl(List<PaymentStrategy> paymentStrategies){
this.strategies = paymentStrategies.stream()
.collect(Collectors.toMap(s -> s.getClass().getSimpleName().toLowerCase(), Function.identity()));
}
public void processPayment(String strategyName,double amount){
PaymentStrategy strategy = strategies.getOrDefault(strategyName,null);
if (strategy != null){
strategy.pay(amount);
}else {
throw new IllegalArgumentException("Strategy not found" + strategyName);
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
4、controller层的调用
import com.hysoft.study.service.impl.PaymentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("payment")
public class PaymentController {
@Autowired
private PaymentServiceImpl paymentService;
@PostMapping("test")
public void test(String paymentname,double amount){
this.paymentService.processPayment(paymentname,amount);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
说明
因此在这里名称传值可以是qqpayment或weixinpayment。因此传值需要提前和前端进行协商。
枚举与策略模式结合
1、创建枚举
public enum OrderStatus {
NEW {
@Override
public void process () {
System.out.println("处理新建订单");
}
},
PAID {
@Override
public void process () {
System.out.println("订单已支付");
}
},
UNPAD {
@Override
public void process() {
System.out.println("订单未支付");
}
};
public abstract void process();
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
2、service层书写处理方法
3、controller层调用
import com.hysoft.study.model.OrderStatus;
import com.hysoft.study.service.impl.OrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("order")
public class OrderController {
@Autowired
private OrderServiceImpl orderService;
@PostMapping("test")
public void test(String status){
OrderStatus aNew = OrderStatus.valueOf(status);
this.orderService.handleOrder(aNew);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
4、说明
Lambda表达式与函数接口
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
@Service
public class StreamServiceImpl {
private final Map<String, Function<Double, Double>> discountFunctions = new HashMap<>();
public StreamServiceImpl() {
discountFunctions.put("VIP1", e -> e * 0.95);
discountFunctions.put("VIP2", e -> e * 0.95 - 20);
}
public double applyDiscount(String vipname, double price) {
Double apply = discountFunctions.getOrDefault(vipname, Function.identity()).apply(price);
return apply;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
在controller层调用时,需要传入vip等级和总计算金额,计算结果时打折后金额
import com.hysoft.study.service.impl.StreamServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("stream")
public class StreamController {
@Autowired
private StreamServiceImpl streamService;
@PostMapping("test")
public double test(String vipname,Double price){
return this.streamService.applyDiscount(vipname,price);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
说明