1、需求+分析
核算每个订单的不同成本项目,每个订单的成本项目通过设置公式算出,而公式里每个公式元素的计算方式都不一样,所以此处使用策略模式就非常合适。
2、实现
枚举:
public enum CostCalElementType {
PUR_DP_COST("外购门板费用", "1001"),
PUR_MAT_COST("其他材料费", "1002"),
ORDER_AMOUNT("订单总金额", "1003"),
ORDER_ACTUAL_AMOUNT("应付总金额", "1004");
private String name;
private String no;
CostCalElementType(String name, String no) {
this.name = name;
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
}
注解:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface CostCalElementAnnotation {
CostCalElementType type();
}
抽象策略类:
public interface CostCalElementStrategy {
Map<String, Float> calculate(Set<String> orderNos);
}
具体策略类:
@Service
@CostCalElementAnnotation(type = CostCalElementType.ORDER_ACTUAL_AMOUNT)
public class ActualAmountStrategy implements CostCalElementStrategy {
@Override
public Map<String, Float> calculate(Set<String> orderNos) {
System.out.println("应付总金额计算");
return Collections.emptyMap();
}
}
@Service
@CostCalElementAnnotation(type = CostCalElementType.ORDER_AMOUNT)
public class OrderAmountStrategy implements CostCalElementStrategy {
@Override
public Map<String, Float> calculate(Set<String> orderNos) {
System.out.println("订单总金额计算");
return Collections.emptyMap();
}
}
@Service
@CostCalElementAnnotation(type = CostCalElementType.PUR_DP_COST)
public class PurDpCalStrategy implements CostCalElementStrategy {
@Override
public Map<String, Float> calculate(Set<String> orderNos) {
System.out.println("外购门板费用计算");
return Collections.emptyMap();
}
}
@Service
@CostCalElementAnnotation(type = CostCalElementType.PUR_MAT_COST)
public class PurMatCalStrategy implements CostCalElementStrategy {
@Override
public Map<String, Float> calculate(Set<String> orderNos) {
System.out.println("其他材料费用计算");
return Collections.emptyMap();
}
}
工厂类:根据具体策略类上CostCalElementAnnotation
注解中 type 属性的值与传入的 CostCalElementType
枚举比对,从工厂类中得到需要的具体策略类。如果从工厂类中找不到对应的策略类则报错!报错导致的原因可能是忘记在具体策略类上加注解或者真的没有这个策略类。
@Component
public class CostCalElementFactory {
private final List<CostCalElementStrategy> costCalElementStrategies;
public CostCalElementFactory(List<CostCalElementStrategy> costCalElementStrategies) {
this.costCalElementStrategies = costCalElementStrategies;
}
public CostCalElementStrategy getCostCalElementStrategy(CostCalElementType costCalElementType) {
return costCalElementStrategies.stream()
.filter(
strategy -> {
Class<? extends CostCalElementStrategy> clazz = strategy.getClass();
if (clazz.isAnnotationPresent(CostCalElementAnnotation.class)) {
CostCalElementAnnotation costCalElementAnnotation =
clazz.getAnnotation(CostCalElementAnnotation.class);
return costCalElementAnnotation.type().equals(costCalElementType);
}
return false;
})
.findFirst()
.orElseThrow(
() -> new RuntimeException(String.format("找不到%s对应的策略!", costCalElementType.getName())));
}
}
测试类:
@SpringBootTest
public class CostCalElementStrategyTest {
@Autowired private CostCalElementFactory costCalElementFactory;
@Test
public void test() {
Map<String, Float> calculate =
costCalElementFactory
.getCostCalElementStrategy(CostCalElementType.PUR_MAT_COST)
.calculate(Collections.emptySet());
}
}