使用java8的lambda作为方法参数以避免冗余

时间:2014-07-07 21:51:11

标签: java lambda java-8

我的班级有很多这样的方法(非常简化)

public Record[] getRecordForXXX(String username, String param1) throws MyException {
    User user = getUser(String username); // Always the same
    MyObject myObj = getMyObject(param1); // Always the same

    // Here is the only line of code, this is different in any of thoose methods
    Record[] recs = getRecords1(user, myObject); // or getRecords2(user, myObject) ...

    // Handle those records always the same ...
    handleRecords(recs); // Always the same
    return recs; // Always the same
}

有没有办法使用lambdas来避免冗余,如:

   public Record[] getRecord(String userName, String param1, XXX method) throws MyException {
        User user = getUser(String username); // Always the same
        MyObject myObj = getMyObject(param1); // Always the same

        // => Call the given 'method' using the parameters user and myObj and returning records

        // Handle those records always the same ...
        handleRecords(recs); // Always the same
        return recs;  // Always the same
   }

我知道,我可以使用某种接口(命令模式)来做到这一点,但我喜欢使用更多功能的方法...... TIA!

3 个答案:

答案 0 :(得分:3)

public Record[] getRecordForXXX(String username, String param1, BiFunction<User, MyObject, Record[]> loader) throws MyException {
    User user = getUser(String username); // Always the same
    MyObject myObj = getMyObject(param1); // Always the same

    Record[] recs = loader.apply(user, myObject);

    // Handle those records always the same ...
    handleRecords(recs); // Always the same
    return recs; // Always the same
}

getRecordForXXX("user", "param1", ClassName::getRecords1);
getRecordForXXX("user", "param1", ClassName::getRecords2);

答案 1 :(得分:2)

试试这个,

public Record[] getRecord(String userName, String param1, BiFunction<User, MyObject, Record[]> method) throws MyException {
    User user = getUser(String username); // Always the same
    MyObject myObj = getMyObject(param1); // Always the same

    Record[] recs = method.apply(user, myObj);

    // Handle those records always the same ...
    handleRecords(recs); // Always the same
    return recs;  // Always the same
}

您可以按如下方式调用新功能:

yourObject.getRecord(userName, param1, (aUserName, aParam1) -> {
    // do some stuff with aUserName and aParam1
    return aRecordArray;
})

答案 2 :(得分:2)

尽管lambdas在这里可能有效,但你不会将它们用于它们的真正用途。您正在寻找strategy pattern,并且枚举可以很好地实现策略模式。

enum Type {

    XXX {

                @Override
                Record[] forType(User user, MyObject obj) {
                    // Something here.
                    return null;
                }
            },
    YYY {

                @Override
                Record[] forType(User user, MyObject obj) {
                    // Something here.
                    return null;
                }
            };

    abstract Record[] forType(User user, MyObject obj);
}

public Record[] getRecord(String userName, String param1, Type type) throws MyException {
    User user = getUser(userName); // Always the same
    MyObject myObj = getMyObject(param1); // Always the same

    // Use the Type to choose the strategy to grow the records.
    Recs recs = type.forType(user, myObj);

    // Handle those records always the same ...
    handleRecords(recs); // Always the same
    return recs;  // Always the same
}