samedi 28 janvier 2017

use java.util.function.Function to implement Factory Design Pattern

Is it correct to use java.util.function.Function to implement Factory Design Pattern

In the following example, I've used Function reference to instantiated a Person type object.

import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<String , Person> myFunc = (String s) -> new Person(s);
        Person p = myFunc.apply("Shan");
        System.out.println(p.getName());
    }
}

class Person{
    private String name;

    public Person(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }


}

Aucun commentaire:

Enregistrer un commentaire