自定义链模型执行流程

目标: 执行有顺序流程任务,可根据业务自动异动流程顺序

上代码吧!!!

1.上下文引用RequestContext.class

public interface RequestContext<V> {

    V toView();
}

2.处理器定义ActionHandle.class

public interface ActionHandle<Ctx extends RequestContext> {

    boolean execute(Ctx request);
}

3.构建处理器BuildDataHandle.class

import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class BuildDataHandle implements ActionHandle<SearchRequestContext> {

    @Override
    public boolean execute(SearchRequestContext requestContext) {

        log.info("构建数据处理器....");
        ShowDataParam showDataParam = new ShowDataParam();
        showDataParam.setIsShow(Boolean.TRUE);
        requestContext.setShowDataParam(showDataParam);

        return true;
    }
}

4.展示处理器ShowDataHandle.class

import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class ShowDataHandle implements ActionHandle<SearchRequestContext> {

    @Override
    public boolean execute(SearchRequestContext requestContext) {
        log.warn("展示数据...");
        ShowDataParam showDataParam = requestContext.getShowDataParam();
        if (Objects.isNull(showDataParam)) {
            return false;
        }

        return true;
    }
}

5.处理器链ActionHandleChain.class

import java.util.ArrayList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ActionHandleChain<Ctx extends RequestContext> {

    /**
     * 处理链
     */
    private List<ActionHandle<RequestContext>> handleChain = new ArrayList<>();

    /**
     * 请求上下文引用
     */
    private Ctx requestContext;

    public ActionHandleChain context(Ctx requestContext) {

        this.requestContext = requestContext;

        return this;
    }

    public ActionHandleChain addHandle(ActionHandle<RequestContext> actionHandle) {

        handleChain.add(actionHandle);
        return this;
    }

    public CallbackImpl submit() {

        for (ActionHandle<RequestContext> handle : handleChain) {
            boolean isSuccess = handle.execute(requestContext);
            if (!isSuccess) {
                log.error("链执行失败,handle:{}", handle);
                throw new RuntimeException("链执行失败");
            }
        }

        return new CallbackImpl(requestContext);
    }

}

6.返回值定义Callback.class

public interface Callback {

    <V> V toView(Class<V> vClass);
}

7.返回值实现定义CallbackImpl.class

public class CallbackImpl implements Callback {

    private RequestContext requestContext;

    public CallbackImpl(RequestContext requestContext) {
        this.requestContext = requestContext;
    }

    public <V> V toView(Class<V> vClass) {

        Object viewObject = requestContext.toView();
        return vClass.cast(viewObject);
    }
}

8.入参:

SearchRequestContext.class


import lombok.Data;

@Data
public class SearchRequestContext implements RequestContext<SearchResponseVO> {

    private ShowDataParam showDataParam;

    @Override
    public SearchResponseVO toView() {
        SearchResponseVO responseVO = new SearchResponseVO();
        responseVO.setMessage("流程结束");

        return responseVO;
    }
}
ShowDataParam.class
import lombok.Data;

@Data
public class ShowDataParam {

    private Boolean isShow;
}

9.出参:

SearchResponseVO.class

import lombok.Data;

@Data
public class SearchResponseVO {

    private String message;
}

10.执行conroller

RestController
public class TestController {

    @Autowired
    private BuildDataHandle buildDataHandle;
    @Autowired
    private ShowDataHandle showDataHandle;

    @GetMapping("/test")
    public SearchResponseVO test() {
        SearchRequestContext searchRequestContext = new SearchRequestContext();
        
       return new ActionHandleChain<>()
                .context(searchRequestContext)
                .addHandle(buildDataHandle)
                .addHandle(showDataHandle)
                .submit()
                .toView(SearchResponseVO.class);
      
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值