JAVA|オブジェクト作成者を関数インタフェースとする


関数インタフェースとしてジェネレータを使用する方法をJAVAで見つけました.
リファレンスサイト

01.Function<T,R>の利用

Function<String, Student> constructor = Student::new; 
Student one = constructor.apply("Han");

class Student {
   private String name;
   public Student(String name) {
      this.name = name;
}

02.@Functional Interface


@FunctinInterface操作は、希望するジェネレータインタフェースをより柔軟に定義して使用できるようです.
@FunctionalInterface
interface MyFunctionalInterface {
   Student getStudent(String name);
}

...

MyFunctionalInterface constructor = Student::new;
Student one = constructor.getStudent("Han"); 

03.私のプロジェクトに適用


受信したメッセージのIDをキー値として使用して、Mapでメッセージオブジェクトジェネレータを管理および使用しようとします.
@FunctionalInterface
interface MessageConstructor {
    Message construct(String[] line);
}
private final Map<String, MessageConstructor> idToConstructorMap = new HashMap<>();
...
idToConstructorMap.put(MessageSpec.SET_TARGET_ALL.id(), TargetResponse::new);