JAVA|オブジェクト作成者を関数インタフェースとする
5694 ワード
関数インタフェースとしてジェネレータを使用する方法をJAVAで見つけました.
リファレンスサイト
@FunctinInterface操作は、希望するジェネレータインタフェースをより柔軟に定義して使用できるようです.
受信したメッセージのIDをキー値として使用して、Mapでメッセージオブジェクトジェネレータを管理および使用しようとします.
リファレンスサイト
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);
Reference
この問題について(JAVA|オブジェクト作成者を関数インタフェースとする), 我々は、より多くの情報をここで見つけました https://velog.io/@joosing/JAVA-객체-생성자를-함수-인터페이스로-다루기テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol