2

I am just trying learn some java-8 stuff and came across this script.

public class ConstructorTest {

    public static void main(String[] args) {
        PersonFactory<Person> personFactory  = Person::new;
        Person person = personFactory.create("Test", "User");
        System.out.println(person);
    }

}

interface PersonFactory<P extends Person> {
    P create(String firstName, String lastName);
}

class Person {

    private String firstName;
    private String lastName;

    Person() {}

    Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; }

    @Override
    public String toString() {
        return "FirstName -> "+firstName + " : LastName ->"+ lastName;
    }

}

Which looks good. But my question is, rather going for this two lines of code

PersonFactory<Person> personFactory  = Person::new;
Person person = personFactory.create("Test", "User");
System.out.println(person);

I can better go can create my Person object in single with new keyword right?

Person person = new Person("Test", "User");

So what is the advantage does this java-8 adding by providing this Constructor References?

Thanks in advance.

  • 4
    This is more about what's the advantage of using the Factory pattern right? – Tunaki Dec 11 '15 at 16:49
  • I had the same doubt, but with `Functional Interface` we can able to have only one abstract method. So there is no point for factory – Suganthan Madhavan Pillai Dec 11 '15 at 16:50
  • Take a look at http://stackoverflow.com/questions/33960921/what-is-the-advantage-of-using-supplier-in-java – hbelmiro Dec 11 '15 at 16:53
  • Note `BiFunction` would work just fine - you don't need your `iterface`. – Boris the Spider Dec 11 '15 at 16:53
  • The number of methods doesn't matter, a factory is a factory. – biziclop Dec 11 '15 at 16:55
  • @BoristheSpider The interface provides clarity and abstraction. – biziclop Dec 11 '15 at 16:55
  • @biziclop which might not needed for simple object creation right? – Suganthan Madhavan Pillai Dec 11 '15 at 16:56
  • 1
    @biziclop that really depends. If you are publishing the `interface` as part of the API then yes. I would argue though, that if the `interface` is internal then there really isn't much need for it. – Boris the Spider Dec 11 '15 at 16:56
  • @BoristheSpider I think that even if it's internal, if its use is non-trivial (e.g. you use it from multiple locations in your code, or you pass it around a lot), you might want a specific interface. That way if you need to change the interface (say, add `middleName` parameter) you won't have to root around to find what effect it would have. But technically it isn't necessary, that's true. – biziclop Dec 11 '15 at 16:59
  • @biziclop changing the interface will break the code just as effectively... But I get your point - _It Depends_ (TM). – Boris the Spider Dec 11 '15 at 17:00
  • @Suganthan What if the factory could return any implementation of an interface? – biziclop Dec 11 '15 at 17:00
  • 2
    @OP I think as Holger says [in his answer](http://stackoverflow.com/a/29387876/2071828) - your example to too trivial to demonstrate the power of using a Factory. You are thinking too much in terms of this example, where indeed, calling `new` would make much more sense. – Boris the Spider Dec 11 '15 at 17:01
  • @BoristheSpider I agreed with both of you. | It’s just the same as with the “hello world” program – Suganthan Madhavan Pillai Dec 11 '15 at 17:04
  • @biziclop Your point makes sense when I think about `middlename` – Suganthan Madhavan Pillai Dec 11 '15 at 17:07

0 Answers0