简体   繁体   中英

Passing constructor as a parameter to a method

I am new to java and started working on constructors. I am seeing few examples where constructor is passed as parameter to a Method. Please tell me what happens when a constructor is passed as a parameter to a method..or suggest me some links where can i get enough knowledge about using constructors

Depending on the purpose why do you need to pass the constructor you may consider passing the instance of Supplier instead (JavaDoc - https://docs.oracle.com/javase/8/docs/api/java/util/function/Supplier.html ). For example you have a method which suppose to create an account and fill everything in it. You can make this method to accept Supplier as a parameter:

public Account createNewAccount(Supplier<Account> accountConstructor) {
    var account = accountConstructor.get();
    account.fillEverything();
    return account;
}

And after that pass constructor to this method either using lambda:

createNewAccount(() -> new UserAccount());

Or using method reference:

createNewAccount(UserAccount::new);

Both variants are working.

Constructors can be passed as arugments to methods using a method reference, somewhat like a function pointer in C++.

See: http://www.baeldung.com/java-8-double-colon-operator

This can be a Function type with one argument or a BiFunction type with two arguments, either way its a lambda returning a class of the type it constructs.

Like Turing85 said though I don't think this is what you want. Passing constructors as parameters is a pretty niche use case. If you just want information on constructors,

https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

Here is an example class that holds two constructors as instance variables and invokes one of them when the constructItem method is called. The first constructor is stored as a Supplier that returns an object of type S and the second takes a Function that takes type T and returns type S .

public class ConstructorWrapper<T, S> {

    private final Supplier<S> construct;
    private final Function<T, S> constructFromObject;

    public ConstructorWrapper(Supplier<S> constructWithNothing, Function<T, S> constructWithObject) {
        this.construct = constructWithNothing;
        this.constructFromObject = constructWithObject;
    }

    public S constructItem(T k) {
        if (k != null) return this.construct.get();
        else return constructFromObject.apply(k);
    }
}

We can use the class like this to wrap creation of ArrayLists from Sets. x is created by invoking the constructor with no parameters and y is created by invoking the constructor with one parameter.

ConstructorWrapper<Set, ArrayList> setToArrayList = new ConstructorWrapper<>(ArrayList::new, ArrayList::new);
ArrayList x = setToArrayList.constructItem(null);
ArrayList y = setToArrayList.constructItem(new HashSet<>());

Or like this to wrap creation of Sets from ArrayLists:

ConstructorWrapper<ArrayList, HashSet> arrayListsToSets = new ConstructorWrapper<>(HashSet::new, HashSet::new);
HashSet x = arrayListsToSets.constructItem(null);
HashSet y = arrayListsToSets.constructItem(new ArrayList<>());

I used raw ArrayLists and Sets because I didn't want to clutter the code with more generics

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM