t777
t777

Reputation: 3329

Use lambdas of java8 as a method parameter to avoid redundancy

I have a class with a lot of methods like this (very simplified):

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
}

Is there any way to use lambdas to avoid the redundancy like:

   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
   }

I know, I can use some kind of interface (command-pattern) to do this, but I like to use a more functionality approach ... TIA!

Upvotes: 3

Views: 164

Answers (3)

OldCurmudgeon
OldCurmudgeon

Reputation: 65793

Although lambdas may be effective here you won't be using them for what they are really for. You are looking for the strategy pattern and enums can make an excellent implementation of a 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
}

Upvotes: 2

ovunccetin
ovunccetin

Reputation: 8663

Try this one,

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
}

You can call your new function as follows:

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

Upvotes: 2

nosid
nosid

Reputation: 50024

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);

Upvotes: 3

Related Questions