java策略模式代码案例

如果大家对策略模式不是很了解的话可以先看前面文章 ~~~>策略模式

以下我们以付款场景为例:
不用币种有不同的结算方式,币种类型和金额都是通过请求参数带入后执行不同的算法,然后返回计算结果. 以下案例适用于所有类似的场景业务

直接上代码:

一:先定义抽象类

/**
 * 结算抽象类
 * @author jianYuan.zhu
 */
public interface SettleInterface {

    /**
     * 此方法用来判断具体执行哪一个实现类
     * @param operation 操作枚举
     * @return
     */
    boolean isSupport(Integer operation);

    /**
     * 结算核心算法
     * @param BigDecimal amount 金额       
     * @param Integer currencyType 币种类型
     */
    String settle(BigDecimal amount , Integer currencyType);
}

二:定义具体实现类

人名币

/**
 * 人名币结算实现类
 * @author jianYuan.zhu
 */
@Slf4j
@Service
public class RmbSettleService implements SettleInterface {

    @Override
    public boolean isSupport(Integer operation) {
        return CurrencyTypeEnum.RMB.code() == operation;
    }

    @Override
    public BigDecimal settle(BigDecimal amount , Integer currencyType){
        BigDecimal result = BigDecimal.ZERO;
         //todo 具体自己的业务逻辑
        return result;
    }
}

美元

/**
 * 美元结算实现类
 * @author jianYuan.zhu
 */
@Slf4j
@Service
public class UsdSettleService implements SettleInterface {

    @Override
    public boolean isSupport(Integer operation) {
        return CurrencyTypeEnum.USD.code() == operation;
    }

    @Override
    public BigDecimal settle(BigDecimal amount , Integer currencyType){
        BigDecimal result = BigDecimal.ZERO;
         //todo 具体自己的业务逻辑
        return result;
    }
}

日元

/**
 * 日元结算实现类
 * @author jianYuan.zhu
 */
@Slf4j
@Service
public class JpySettleService implements SettleInterface {

    @Override
    public boolean isSupport(Integer operation) {
        return CurrencyTypeEnum.JPY.code() == operation;
    }

    @Override
    public BigDecimal settle(BigDecimal amount , Integer currencyType){
        BigDecimal result = BigDecimal.ZERO;
         //todo 具体自己的业务逻辑
        return result;
    }
}

枚举类

/**
 * 货币枚举类
 */
@Getter
public enum CurrencyTypeEnum{
    RMB(1, "人民币"),
    USD(2, "美元"),
    JPY(3, "日元");
    

    private final Integer code;
    private final String value;

    CurrencyTypeEnum(Integer code, String value) {
        this.code = code;
        this.value = value;
    }

    public int code() {
        return code;
    }

    public String value() {
        return value;
    }

    public static String getValueByCode(Integer code) {
        if (code == null) return null;
        for (CurrencyTypeEnum enums : CurrencyTypeEnum.values()) {
            if (Objects.equals(enums.getCode(),code)) {
                return enums.getValue();
            }
        }
        return null;
    }
}

三: 上下文

/**
 * 结算上下文
 * @author jianYuan.zhu
 */
@Component
public class SettleOperationContext {
    private final List<SettleInterface> settleInterfaces;

    public SettleOperationContext (List<SettleInterface> settleInterfaces) {
        this.settleInterfaces = settleInterfaces ;
    }

    public BigDecimal settleAmount(BigDecimal amount ,Integer currencyType) {
        for (SettleInterface settleInterface: settleInterfaces) {
             if (settleInterface.isSupport(currencyType)){
                 return  settleInterface.settle(amount );
             }
        }
        return "没有符合的操作类型";
    }
}

四: 模拟调用代码

/**
 * @author jianYuan.zhu
 */
@RestController
public class SettleController {

    @Autowired
    private SettleOperationContext settleOperationContext ;

    @PostMapping("/settle")
    public String settleAmount(@RequestBody @Validated SettleReq settleReq) throws Exception {
       return settleOperationContext (settleReq.getAmount(), settleReq.getCurrencyType())
    }
}

以上便是完整的一个策略模式代码案例,平时工作中的类似业务场景也可以稍加改善就可以使用.

你可能感兴趣的:(工作实用,java,策略模式)