如何少用switch,多用strategy

-第一步操作使用算法的角色,维护抽象策略类的引用:

/**
 * @Title :环境类【使用算法的角色,维护抽象策略类的引用】
 * @Author :hangyu
 * @Date :2020/7/15
 * @Version:1.0
 */
public interface OrderServiceProvider {

    OrderService getOrderService(Long orderType);
}

@Service
public class OrderServiceProviderImpl implements OrderServiceProvider {

    @Autowired
    private List orderServices;

    @Override
    public OrderService getOrderService(Long orderType) {
        for (OrderService orderService : orderServices){
            if (orderService.strategy(orderType)){
                return orderService;
            }
        }
        throw new IllegalArgumentException("strategy fail orderType is "+ orderType);
    }
}

-第二步操作声明抽象算法:

/**
 * @Title :抽象策略类【声明抽象算法,方便替换算法】
 * @Author :hangyu
 * @Date :2020/7/15
 * @Version:1.0
 */
public interface OrderService {

    /**
     * 匹配策略
     * @param orderType
     * @return
     */
    boolean strategy(Long orderType);

    /**
     * 创建订单
     * @param order
     */
    void createOrder(Order order);
}

public class Order implements Serializable {

    private static final long serialVersionUID = -1488118862353539472L;

    private Long orderType;

    private String orderDesc;


    public Long getOrderType() {
        return orderType;
    }

    public void setOrderType(Long orderType) {
        this.orderType = orderType;
    }

    public String getOrderDesc() {
        return orderDesc;
    }

    public void setOrderDesc(String orderDesc) {
        this.orderDesc = orderDesc;
    }
}

public enum OrderTypeEnum {

    GENERAL_ORDER(1L,"普通订单"),
    GROUP_ORDER(2L,"团购订单");

    private Long orderType;

    private String orderDesc;

    OrderTypeEnum(Long orderType, String orderDesc) {
        this.orderType = orderType;
        this.orderDesc = orderDesc;
    }

    public Long getOrderType() {
        return orderType;
    }

    public void setOrderType(Long orderType) {
        this.orderType = orderType;
    }

    public String getOrderDesc() {
        return orderDesc;
    }

    public void setOrderDesc(String orderDesc) {
        this.orderDesc = orderDesc;
    }
}

-第三步操作实现抽象策略类:

/**
 * @Title :具体策略类:[实现抽象策略类]
 * @Author :hangyu
 * @Date :2020/7/15
 * @Version:1.0
 */
public class GeneralOrderServiceImpl implements OrderService {

    @Override
    public boolean strategy(Long orderType) {
        return OrderTypeEnum.GENERAL_ORDER.getOrderType().equals(orderType);
    }

    @Override
    public void createOrder(Order order) {
        System.out.println(order.getOrderDesc() + "创建成功");
    }
}

/**
 * @Title :具体策略类:[实现抽象策略类]
 * @Author :hangyu
 * @Date :2020/7/15
 * @Version:1.0
 */
public class GroupOrderServiceImpl implements OrderService {

    @Override
    public boolean strategy(Long orderType) {
        return OrderTypeEnum.GROUP_ORDER.getOrderType().equals(orderType);
    }

    @Override
    public void createOrder(Order order) {
        System.out.println(order.getOrderDesc() + "创建成功");
    }
}

-第四步接口调用:

public class OrderCreateMain {

    @Autowired
    private OrderServiceProvider orderServiceProvider;

    public void createOrder(Long orderType,String orderDesc){
        OrderService orderService = orderServiceProvider.getOrderService(orderType);
        Order order = new Order();
        order.setOrderDesc(orderDesc);
        order.setOrderType(orderType);
        orderService.createOrder(order);
    }
}

原理是利用多态代替if else switch条件判断,如果方法有缓存用的时候需要注意。

你可能感兴趣的:(如何少用switch,多用strategy)