以几乎相同的方法合并重复的代码

时间:2016-02-17 19:30:05

标签: java

我如何将大部分代码移动到一个函数中或以其他方式合并它?我对这么多重复的代码不太满意。事件IntelliJ抱怨它......

public boolean closeTrade(Trade trade) {
    for (int i=0; i< NUM_CHECKS; i++) {
        if (closeBrokerTrade(trade)) return true; // quit loop if successfully closed

        //region sleep between checks
        if (i < NUM_CHECKS -1) try {
            Thread.sleep(DELAY);
        } catch (InterruptedException e) {
            throw propagate(e);
        }
        //endregion
    }
    return false;
}


public boolean closeTrade(String ticket) {
    for (int i=0; i< NUM_CHECKS; i++) {
        if (closeBrokerTrade(ticket)) return true; // quit loop if successfully closed

        //region sleep between checks
        if (i < NUM_CHECKS -1) try {
            Thread.sleep(DELAY);
        } catch (InterruptedException e) {
            throw propagate(e);
        }
        //endregion
    }
    return false;
}

protected abstract boolean closeBrokerTrade(Trade trade);
protected abstract boolean closeBrokerTrade(String ticket);

3 个答案:

答案 0 :(得分:2)

在Java 8中,您可以将正确版本的closeBrokerTrade作为lambda传递:

声明这样的函数:

    private void Split_btn_Click(object sender, EventArgs e)
    {
        string s = label1.Text;
        // Split string on spaces.
        // ... This will separate all the words.
        string[] words = s.Split('x');
        foreach (string word in words)
        {
            Left_txtbox.Text = word;
            Right_Textbox.Text = word;
        }

    }

并称之为:

public boolean closeTrade( BooleanSupplier f) {
    // ...
    if (f.getAsBoolean()) return true; // quit loop if successfully closed
    // ...
    return false;
}

答案 1 :(得分:1)

我需要看看closeBrokerTrade的实现以确定行为,但我会这样做:

public boolean closeTrade(Trade trade) {
    String ticket = ...// generate ticket from trade in whatever way you do it, e.g. trade.getTicket() or trade.toString(), etc. etc.
    return closeTrade(ticket);
}


public boolean closeTrade(String ticket) {
    for (int i=0; i< NUM_CHECKS*3; i++) {
        if (closeBrokerTrade(ticket)) return true; // quit loop if successfully closed

        //region sleep between checks
        if (i < NUM_CHECKS -1) try {
            Thread.sleep(DELAY);
        } catch (InterruptedException e) {
            throw propagate(e);
        }
       //endregion
    }
    return false;
}

如果我错了,或者你是从票中获得交易,请纠正我。

如果那是不可能的,这是我的建议: 使用泛型来表示参数化的交易结束类,如下所示:

public abstract class ClosingTradeStrategy<T> {
    public boolean closeTrade(T trade) {
        for (int i=0; i< NUM_CHECKS*3; i++) {
            if (closeBrokerTrade(trade)) return true; // quit loop if successfully closed

            //region sleep between checks
            if (i < NUM_CHECKS -1) try {
                Thread.sleep(DELAY);
            } catch (InterruptedException e) {
                throw propagate(e);
            }
           //endregion
        }
        return false;
    }

    protected abstract boolean closeBrokerTrade(T trade);
}

然后您可以使用它来实现不同的交易结算策略:

public class StringClosingTradeStrategy extends ClosingTradeStrategy<String> {
    @Override
    protected boolean closeBrokerTrade(String trade) {
        ... // implement
    }
}

public class TradeClosingTradeStrategy extends ClosingTradeStrategy<Trade> {
    @Override
    protected boolean closeBrokerTrade(Trade trade) {
        ... // implement
    }
}

第二种方法的优点是可以轻松扩展到其他结算策略。

答案 2 :(得分:1)

使用界面,就像使用Comparator<T>

一样
private static interface CloseBrokerTradeChecker <T> {
    boolean closeBrokerTrade(T t);
}

private static class CloseBrokerTradeCheckerTrade 
    implements CloseBrokerTradeChecker<Trade> {
    @Override
    boolean closeBrokerTrade(Trade trade) { ... } 
}

private static class CloseBrokerTradeCheckerTicket 
    implements CloseBrokerTradeChecker<String> {
    @Override
    boolean closeBrokerTrade(String ticket) { ... } 
}

private <T> boolean closeTrade(T t, CloseBrokerTradeChecker<T> checker) {
    for (int i=0; i< NUM_CHECKS*3; i++) {
        if (checker.closeBrokerTrade(t)) return true; // quit loop if successfully closed

        //region sleep between checks
        if (i < NUM_CHECKS -1) try {
            Thread.sleep(DELAY);
        } catch (InterruptedException e) {
            throw propagate(e);
        }
        //endregion
    }
    return false;
}

public boolean closeTrade(Trade trade) {
    // TODO: extract CloseBrokerTradeCheckerTrade as a static final variable
    return closeTrade(trade, new CloseBrokerTradeCheckerTrade());
}

public boolean closeTrade(String ticket) {
    return closeTrade(ticket, new CloseBrokerTradeCheckerTicket());
}