Java设计模式-责任链模式,应用接口多个参数验证,订单多个费用的计算

2024-10-09 11:17:32 浏览数 (2)

Java设计模式-责任链模式,应用接口多个参数验证,订单多个费用的计算

1.定义请求和返回对象的上下文对象

代码语言:javascript复制
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.定义异常对象

代码语言:javascript复制
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.定义接口

代码语言:javascript复制
package com.example.core.mydemo.java.filter;



public interface OrderFilter {
    /**
     * 检查该订单的请求是否合法,如果不合法,将抛出OrderFilterException
     * @param requestContext
     */
    public void validate(OrderReqContext requestContext) throws OrderFilterException;
}

4.定义责任链类

代码语言:javascript复制
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

代码语言:javascript复制
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

代码语言:javascript复制
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.测试类

代码语言:javascript复制
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;
    }
}

0 人点赞