其他分享
首页 > 其他分享> > 系统代码重构

系统代码重构

作者:互联网

业务背景

以RabbitMQ接收消息,消费消息为例。
接收不同消息类型通知。做相应处理。

代码设计

设计模式:工厂模式+状态模式+策略模式
工厂:MessageReceiveProcessorTypeBServiceContext
状态:TYPEA-业务ATYPEB-业务BTYPEC-业务CTYPED-业务D
策略:TYPEB-业务B业务B-1TYPEB-业务B业务B-2
枚举:enum Action
泛型:T data
说明:将不同的消息类型的接收写在同一个工厂类MessageReceiveProcessor类中,根据不同的状态Action枚举值、且定义泛型T data接收不同消息类型的消息数据,跳转到不同的sevice中处理。
其中TYPEB-业务B消息处理TypeBServiceContext工厂,因TYPEB-业务B对于每一个TYPEB有不同的B-1/B-2处理。故对于同一个TYPEB-业务B状态,在工厂类TypeBServiceContext中根据”对应依据“策略到不同的service中去做不同的B-1/B-2处理。

微信交流群:Java技术沟通群⑤(点击加入)

枚举类ReceiveMessage

@Data
public class ReceiveMessage<T extends Serializable> {
    private String traceId;
    private String action;
    private T data;

    public static enum Action {
        TYPE_A("TYPEA", "业务A"),
        TYPE_B("TYPEB", "业务B"),
        TYPE_C("TYPEC", "业务C"),
        TYPE_D("TYPED", "业务D"),
        ;
        private String code;
        private String desc;

        private Action(String code, String desc) {
            this.code = code;
            this.desc = desc;
        }

        public static Action getAction(String code){
            if(code == null){
                return TYPE_B; //默认
            }
            for (Action action : values()) {
                if (action.code.equals(code)) {
                    return action;
                }
            }

            return TYPE_B;
        }
    }
}

消息接收工厂MessageReceiveProcessor

/**
 * MQ接收消息工厂
 */
@Slf4j
@Component("MessageReceiveProcessor")
public class MessageReceiveProcessor {
    @Resource
    IXXXService xxxService;

    @Resource
    TypeBServiceContext typeBServiceContext;

    public void receiveMessage(String message) {
        log.info("MQ接收业务数据入参, req: [{}]", message);
        ReceiveMessage requestDTO = JSON.parseObject(message, ReceiveMessage.class);
        try {
            switch (ReceiveMessage.Action.getAction(requestDTO.getAction())) {
                case TYPE_A:
                    xxxService.typeA(JSON.parseObject(requestDTO.getData().toString(), A.class));
                    break;
                case TYPE_B:
                     TypeBResponseDTO resp = typeBServiceContext.getTypeBService(StateEnum.B_1.getCode()).dealB(JSON.parseObject(requestDTO.getData().toString(), TypeBResponseDTO.class));
                    log.info("typeB, resp:[{}]", JSON.toJSONString(resp));
                    break;
                case TYPE_C:
                    xxxService.typeC(JSON.parseObject(requestDTO.getData().toString(), BRequestDTO.class));
                    break;
                case TYPE_D:      
                    xxxService.typeB(JSON.parseObject(requestDTO.getData().toString(), A.class));
                    break;

            }
        } catch (Exception e) {
            log.error("MQ接收消息初始化数据异常", e);
        } finally {
        //
        }

    }
}

状态-TYPEB-业务B-策略工厂NoticeMakeLoanStatusServiceContext

@Component
public class TypeBServiceContext {

    @Resource(name = "B-1Service")
    IBService b-1Service;
    @Resource(name = "B-2Service")
    IBService b-2Service;
    
    private Map<String,IBService> statusMap = new HashMap<>();
    @PostConstruct
    void init(){
        statusMap.put(StateEnum.B_1.getCode(),b-1Service);
        statusMap.put(StateEnum.B_2.getCode(),b-2Service);
      
    }
    public IBService getTypeBService(String code) throws Exception{
        IBService typeBService = statusMap.get(code);
        if(typeBService == null){
            throw new RainstormException("没有匹配的处理类");
        }
        return typeBService;
    }

}

状态-TYPEB-业务B-抽象类IBService

/**
 * TYPEB-业务B抽象处理类
 * 
 */
 @Slf4j
public abstract class IBService {

    /**
     * 业务B处理
     *
     */
    public TypeBResponseDTO dealB(RequestDTO requestDTO) {
        return null;
    }
    ...
}

标签:重构,code,String,代码,系统,TYPEB,class,TYPE,public
来源: https://www.cnblogs.com/henlan/p/16554162.html