编程语言
首页 > 编程语言> > java-处理继承时删除条件语句

java-处理继承时删除条件语句

作者:互联网

我想避免使用条件,并应用一些技术来使代码在处理继承的类时更易于维护和扩展.

在这个例子中,我们有.

MarketOrder和LimitOrder都从基类Order扩展.

TradeServiceImpl和getUnitPrice方法,用于计算2个订单将要交易的价格.

此方法有几个条件,可以为不同类型的订单选择正确的价格.此外,它正在转换为派生类.

这种方法的问题在于,如果我要添加5个新Order,则我将拥有7 * 7 = 49 if语句,在其中一些语句中,我将需要转换为一个我可能会与另一个类混淆的派生类.易于编写错误代码,并给我重新设计的感觉.

请问我有什么想法吗?

// Domains.

public class Order{

private OrderTypeEnum orderType; 
// can be buy or sell

public OrderTypeEnum getOrderType() {
    return orderType;
}
public void setOrderType(OrderTypeEnum orderType) {
    this.orderType = orderType;
}
}

public class LimitOrder extends Order{

private Long unitPrice;

public Long getUnitPrice() {
    return unitPrice;
}
public void setUnitPrice(Long unitPrice) {
    this.unitPrice = unitPrice;
}
}

public class MarketOrder extends Order{

}

// Service.

public class TradeServiceImpl implements TradeService{

private TradeRepository tradeRepository;    


public Long getUnitPrice(Order buyOrder, Order sellOrder){

    if(buyOrder instanceof MarketOrder && sellOrder instanceof MarketOrder){
        return tradeRepository.getLastPrice();
    }

    if(buyOrder instanceof MarketOrder && sellOrder instanceof LimitOrder){
        return ((LimitOrder)sellOrder).getUnitPrice();
    }

    if(buyOrder instanceof LimitOrder && sellOrder instanceof MarketOrder){
        return ((LimitOrder)buyOrder).getUnitPrice();
    }

    if(buyOrder instanceof LimitOrder && sellOrder instanceof LimitOrder){
        return ((LimitOrder)buyOrder).getUnitPrice() + ((LimitOrder)sellOrder).getUnitPrice() /2;
    }

    return 0L;
}
}

解决方法:

首先,有两种情况可以合并为一种:当顺序不同时:

 if(buyOrder instanceof MarketOrder && sellOrder instanceof LimitOrder){
        return ((LimitOrder)sellOrder).getUnitPrice();
    }

    if(buyOrder instanceof LimitOrder && sellOrder instanceof MarketOrder){
        return ((LimitOrder)buyOrder).getUnitPrice();
    }

至:

if(!buyOrder.getClass().equals(LimitOrder.getClass())){
        return ((LimitOrder)sellOrder).getUnitPrice();
    }

另一方面,可以将订单类用作包含函数对象的映射的索引.这样,您只需在地图上添加元素即可扩展功能.

您可以使用实现接口的匿名内部类封装计算算法,例如:

public interface IFunction {
    public Long execute(Order oA, Order oB);
}

并使用订单类来决定执行哪些行为来访问此地图:

Map<Class, Map<Class, IFunction>> opClass2calcAlgorithm = new HashMap();

IFunction market_market = new IFunction() {

            @Override
            public Long execute(Order a, Order b) {
                return tradeRepository.getLastPrice();
            }
        };

IFunction market_limit = new IFunction() {

            @Override
            public Long execute(Order a, Order b) {
                return ((LimitOrder)a).getUnitPrice();
            }
        };

Map<Class, IFunction> marketMap = new HashMap();
marketMap.put(MarketOrder.class, market_market);
marketMap.put(LimitOrder.class, market_limit);
opClass2calcAlgorithm.put(marketMap);

最后,您的getUnitPrice方法可以这样实现:

public Long getUnitPrice(Order buyOrder, Order sellOrder){

    long ret = 0L;

    Map<Class, IFunction> firstLevel = opClass2calcAlgorithm.get(buyOrder.getClass());
    if(firstLevel == null) return ret;
    IFunction calcAlg = firstLevel.get(sellOrder.getClass());
    if(calcAlg == null) return ret;

    ret = calcAlg.execute(buyOrder, sellOrder);

    return ret;
}

标签:oop,java,design-patterns,refactoring
来源: https://codeday.me/bug/20191121/2054771.html