使用注解 + 策略模式,减少代码中的if/switch语句,增加代码灵活性

需求描述
现在我们有一个接受供应商订单状态通知的接口,目前支持的功能有退款,改签,后续可能需要接入新功能,比如航变,订单状态改变之类。接口的请求格式一致,接口如下:

基本解决方案:使用switch或if

请求

package com.ahut.contract.flight;

/**
 * @desc : 供应商通知请求
 * @author : cheng
 * @date : 2019-03-04 21:18
 */
public class VendorOrderNotifyRequest {

    /**
     * 数据
     */
    private String data;
    /**
     * 依据tag判断事件类型
     */
    private String tag;
    /**
     * 创建时间
     */
    private long createTime;
    /**
     * 签名
     */
    private String sign;

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }
}



响应

package com.ahut.contract.flight;

/**
 * @desc : 供应商通知响应
 * @author : cheng
 * @date : 2019-03-04 21:21
 */
public class VendorOrderNotifyResponse {

    private int code;

    private String message;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}


请求统一入口接口

package com.ahut.flight.service;

import com.ahut.contract.flight.VendorOrderNotifyRequest;
import com.ahut.contract.flight.VendorOrderNotifyResponse;

/**
 * @desc :
 * @author : cheng
 * @date : 2019-03-04 21:16
 */
public interface VendorOrderNotifyService {

    /**
     * @desc : 
     * @author : cheng
     * @date : 2019-03-04 21:24
     */
    VendorOrderNotifyResponse orderNotify(VendorOrderNotifyRequest request);

}



使用if/switch

package com.ahut.flight.service.impl;

import com.ahut.contract.flight.VendorOrderNotifyRequest;
import com.ahut.contract.flight.VendorOrderNotifyResponse;
import com.ahut.flight.service.OrderNotifyContextService;
import com.ahut.flight.service.OrderNotifyHandleService;
import com.ahut.flight.service.VendorOrderNotifyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class VendorOrderNotifyServiceImpl implements VendorOrderNotifyService {

    /**
     * @desc :
     * @author : cheng
     * @date : 2019-03-04 21:25
     */
    @Override
    public VendorOrderNotifyResponse orderNotify(VendorOrderNotifyRequest request) {
        String tag = Optional.ofNullable(request).map(VendorOrderNotifyRequest::getTag).orElse("");
        if (StringUtils.equals(tag, "change")) {
            // 改签
        } else if (StringUtils.equals(tag, "refund")) {
            // 退款
        } else {
            // 不支持
        }

        switch (tag) {
            case "change":
                // 改签
                break;
            case "refund":
                // 退款
                break;
                default:
                    // 不支持
        }
        return null;
    }

}


缺点:代码不灵活,下次新增功能时,需要修改现有代码(增加if分支或者switch分支)

优化:使用注解 + 策略模式


定义注解

package com.ahut.common.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @desc :
 * @author : cheng
 * @date : 2019-03-04 21:56
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestTag {

    public String value() default "";

}


定义处理请求data的统一接口

package com.ahut.flight.service;

import com.ahut.contract.flight.VendorOrderNotifyResponse;

/**
 * @desc : 处理请求
 * @author : cheng
 * @date : 2019-03-04 21:29
 */
public interface OrderNotifyHandleService {

    /**
     * @desc :
     * @author : cheng
     * @date : 2019-03-04 21:46
     */
    VendorOrderNotifyResponse orderNotify(String data);

}


实现统一接口,退款,使用自定义注解

package com.ahut.flight.service.impl;


import com.ahut.common.annotation.RequestTag;
import com.ahut.contract.flight.VendorOrderNotifyResponse;
import com.ahut.flight.service.OrderNotifyHandleService;
import org.springframework.stereotype.Service;

/**
 * @desc : 退款
 * @author : cheng
 * @date : 2019-03-04 21:50
 */
@Service("RefundNotifyHandleServiceImpl")
@RequestTag("refund")
public class RefundNotifyHandleServiceImpl implements OrderNotifyHandleService {

    /**
     * @desc : 
     * @author : cheng
     * @date : 2019-03-04 22:11
     */
    @Override
    public VendorOrderNotifyResponse orderNotify(String data) {
        VendorOrderNotifyResponse response = new VendorOrderNotifyResponse();
        response.setCode(0);
        response.setMessage("处理退票通知");
        return response;
    }
}


实现统一接口,改签,使用自定义注解

package com.ahut.flight.service.impl;

import com.ahut.common.annotation.RequestTag;
import com.ahut.contract.flight.VendorOrderNotifyResponse;
import com.ahut.flight.service.OrderNotifyHandleService;
import org.springframework.stereotype.Service;

/**
 * @desc : 改签
 * @author : cheng
 * @date : 2019-03-04 21:50
 */
@Service("ChangeNotifyHandleServiceImpl")
@RequestTag("change")
public class ChangeNotifyHandleServiceImpl implements OrderNotifyHandleService {

    /**
     * @desc :
     * @author : cheng
     * @date : 2019-03-04 22:04
     */
    @Override
    public VendorOrderNotifyResponse orderNotify(String data) {
        VendorOrderNotifyResponse response = new VendorOrderNotifyResponse();
        response.setCode(0);
        response.setMessage("处理改签通知");
        return response;
    }
}


实现统一接口,不支持的tag,使用自定义注解

package com.ahut.flight.service.impl;

import com.ahut.common.annotation.RequestTag;
import com.ahut.contract.flight.VendorOrderNotifyResponse;
import com.ahut.flight.service.OrderNotifyHandleService;
import org.springframework.stereotype.Service;

/**
 * @desc : 不支持
 * @author : cheng
 * @date : 2019-03-05 22:11
 */
@Service("NonSupportNotifyHandleServiceImpl")
@RequestTag("nonsupport")
public class NonSupportNotifyHandleServiceImpl implements OrderNotifyHandleService {

    /**
     * @desc :
     * @author : cheng
     * @date : 2019-03-05 22:12
     */
    @Override
    public VendorOrderNotifyResponse orderNotify(String data) {
        VendorOrderNotifyResponse response = new VendorOrderNotifyResponse();
        response.setCode(0);
        response.setMessage("不支持的通知");
        return response;
    }
}


定义上下文接口,依据tag,返回具体的处理实例

package com.ahut.flight.service;

/**
 * @desc : 上下文
 * @author : cheng
 * @date : 2019-03-04 21:27
 */
public interface OrderNotifyContextService {

    /**
     * @desc :
     * @author : cheng
     * @date : 2019-03-04 22:04
     */
    OrderNotifyHandleService getContext(String tag);

}


上下文实现

package com.ahut.flight.service.impl;

import com.ahut.common.annotation.RequestTag;
import com.ahut.flight.service.OrderNotifyContextService;
import com.ahut.flight.service.OrderNotifyHandleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @desc : 上下文
 * @author : cheng
 * @date : 2019-03-04 22:05
 */
@Service
public class OrderNotifyContextServiceImpl implements OrderNotifyContextService {

    @Autowired
    private List handleServiceList;

    @Resource(name = "NonSupportNotifyHandleServiceImpl")
    private OrderNotifyHandleService nonsupportService;

    @Override
    public OrderNotifyHandleService getContext(String tag) {
        if (StringUtils.isBlank(tag)) {
            return nonsupportService;
        }

        // 对比注解中的value和tag
        OrderNotifyHandleService handleService = handleServiceList.stream()
                .filter(f -> StringUtils.equals(tag, f.getClass().getAnnotation(RequestTag.class).value()))
                .findFirst()
                .orElse(nonsupportService);

        return handleService;
    }
}


实现请求统一入口

package com.ahut.flight.service.impl;

import com.ahut.contract.flight.VendorOrderNotifyRequest;
import com.ahut.contract.flight.VendorOrderNotifyResponse;
import com.ahut.flight.service.OrderNotifyContextService;
import com.ahut.flight.service.OrderNotifyHandleService;
import com.ahut.flight.service.VendorOrderNotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class VendorOrderNotifyServiceImpl implements VendorOrderNotifyService {

    /**
     * 持有上下文
     */
    @Autowired
    private OrderNotifyContextService contextService;

    /**
     * @desc :
     * @author : cheng
     * @date : 2019-03-04 21:25
     */
    @Override
    public VendorOrderNotifyResponse orderNotify(VendorOrderNotifyRequest request) {
        String tag = Optional.ofNullable(request).map(VendorOrderNotifyRequest::getTag).orElse("");
        OrderNotifyHandleService handleService = contextService.getContext(tag);
        VendorOrderNotifyResponse response = handleService.orderNotify(request.getData());
        return response;
    }

}


总结
增加了代码的灵活性
符合开闭原则
新增功能时,不用修改原有的代码,直接实现接口即可
--------------------- 
原文链接:https://blog.csdn.net/qq_28988969/article/details/88209001 

你可能感兴趣的:(设计模式)