Java設計模式-責任鏈模式,應用介面多個引數驗證,訂單多個費用的計算

oktokeep發表於2024-04-21

Java設計模式-責任鏈模式,應用介面多個引數驗證,訂單多個費用的計算

1.定義請求和返回物件的上下文物件

package com.example.core.mydemo.java.filter;

import java.time.LocalDateTime;

public class OrderReqContext {
    /**
     * 租客會員號
     */
    String memNo;
    /**
     * 訂單號
     */
    String orderNo;

    /**
     * 開始時間
     */
    private LocalDateTime requestTime;

    /**
     * 返回物件
     */
    private String responseText;

    public String getResponseText() {
        return responseText;
    }

    public void setResponseText(String responseText) {
        this.responseText = responseText;
    }

    public String getMemNo() {
        return memNo;
    }

    public void setMemNo(String memNo) {
        this.memNo = memNo;
    }

    public String getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }

    public LocalDateTime getRequestTime() {
        return requestTime;
    }

    public void setRequestTime(LocalDateTime requestTime) {
        this.requestTime = requestTime;
    }
}

2.定義異常物件

package com.example.core.mydemo.java.filter;

public class OrderFilterException extends RuntimeException{
    private String errorCode;
    /**
     * 異常的描述資訊
     */
    private String errorMsg;
    /**
     * 異常可能附加的物件,方便前端輸出展示
     */
    private Object extra;

    public OrderFilterException(String errorCode, String errorMsg,Throwable cause) {
        super(cause);
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
        this.extra = null;
    }

    public OrderFilterException(String errorCode, String errorMsg,Object extra,Throwable cause) {
        super(cause);
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
        this.extra = extra;
    }

    public OrderFilterException(String errorCode, String errorMsg,Object extra) {
        super(errorMsg);
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
        this.extra = extra;
    }

    public OrderFilterException(String errorCode,String errorMsg){
        super(errorMsg);
        this.errorCode =errorCode;
        this.errorMsg =errorMsg;
    }


    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public Object getExtra() {
        return extra;
    }

    public void setExtra(Object extra) {
        this.extra = extra;
    }
}

3.定義介面

package com.example.core.mydemo.java.filter;



public interface OrderFilter {
    /**
     * 檢查該訂單的請求是否合法,如果不合法,將丟擲OrderFilterException
     * @param requestContext
     */
    public void validate(OrderReqContext requestContext) throws OrderFilterException;
}

4.定義責任鏈類

package com.example.core.mydemo.java.filter;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;


@Service
public class OrderFilterChain implements OrderFilter, ApplicationContextAware {

    private List<OrderFilter> orderFilterList = new ArrayList<>();
    private ApplicationContext applicationContext;

    /**
     * 單元測試類模擬
     */
    public void initTest(){
        /* 引數檢測 */
        orderFilterList.add(new ParamsFilter());
        /** 節假日檢測 **/
        orderFilterList.add(new HolidayFilter());
        /** other其他 **/
    }

    /**
     * 容器啟動時候載入
     */
    @PostConstruct
    private void init(){
        /* 引數檢測 */
        orderFilterList.add(applicationContext.getBean(ParamsFilter.class));
        /** 節假日檢測 **/
        orderFilterList.add(applicationContext.getBean(HolidayFilter.class));
        /** other其他 **/
    }

    @Override
    public void validate(OrderReqContext context) throws OrderFilterException {
       if(orderFilterList.size()==0){
           throw new RuntimeException("cannot be init");
       }
       for(OrderFilter filter: orderFilterList){
           filter.validate(context);
       }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

}

5.實現類1

package com.example.core.mydemo.java.filter;

import com.google.gson.Gson;
import org.springframework.stereotype.Service;

@Service("holidayFilter")
public class HolidayFilter implements OrderFilter {
    @Override
    public void validate(OrderReqContext requestContext) throws OrderFilterException{
        requestContext.setResponseText("節假日");
        System.out.println("節假日檢測完成,引數=" + new Gson().toJson(requestContext));

    }
}

6.實現類2

package com.example.core.mydemo.java.filter;

import com.google.gson.Gson;
import org.springframework.stereotype.Service;

@Service("paramsFilter")
public class ParamsFilter implements OrderFilter {

    @Override
    public void validate(OrderReqContext requestContext) throws OrderFilterException{
        requestContext.setResponseText("引數");
        System.out.println("引數檢測完成,引數="+ new Gson().toJson(requestContext));
    }
}

7.測試類

package com.example.core.mydemo.java.filter;

import java.time.LocalDateTime;

/**
 * 引數檢測完成,引數={"memNo":"123111","orderNo":"DD123456789","requestTime":{"date":{"year":2024,"month":4,"day":19},"time":{"hour":17,"minute":50,"second":57,"nano":943000000}},"responseText":"引數"}
 * 節假日檢測完成,引數={"memNo":"123111","orderNo":"DD123456789","requestTime":{"date":{"year":2024,"month":4,"day":19},"time":{"hour":17,"minute":50,"second":57,"nano":943000000}},"responseText":"節假日"}
 * 返回物件=節假日
 */
public class FilterTest {
    public static void main(String[] args) {
        OrderFilterChain orderFilterChain = new OrderFilterChain();
        //初始化
        orderFilterChain.initTest();
        //前置檢查
        OrderReqContext orderReqContext = initContext();
        //驗證引數
        orderFilterChain.validate(orderReqContext);
        System.out.println("返回物件=" + orderReqContext.getResponseText());
    }

    private static OrderReqContext initContext() {
        OrderReqContext orderReqContext = new OrderReqContext();
        orderReqContext.setOrderNo("DD123456789");
        orderReqContext.setMemNo("123111");
        orderReqContext.setRequestTime(LocalDateTime.now());
        return orderReqContext;
    }
}

相關文章