検証マップを注入する必要があるさまざまな StudentValidator クラス インスタンスを (指定されたパラメーターに基づいて) 作成することが想定されている抽象ファクトリ クラス StudentValidatorFactory があります (以下のコードを参照)。
public class StudentValidatorFactory{
public static final int JUNIOR_STUDENT_TYPE = 1;
public static final int SENIOR_STUDENT_TYPE = 2;
public StudentValidator createStudentValidator(int studentType) throws StudentValidatorCreationException{
Map<String,ValidationBean> validationMap = readValiationMapFromPersistentOrCachedStorage(studentType);
switch (studentType){
case JUNIOR_STUDENT:
return new JuniorStudentValidator(validationMap);
case SENIOR_STUDENT:
return new SeniorStudentValidator(validationMap);
}
}
}
public interface StudentValidator{
void validate(Student student) throws StudentValidationException;
}
public class JuniorStudentValidator{
private Map<String, ValidationBean> validationMap;
public JuniorStudentValidator(Map<String,ValidationBean> validationMap){
this.validationMap = validationMap;
}
public void validate(Student student) throws StudentValidationException{
// make use of validation map for apply junior student related validations on the student
}
}
public class SeniorStudentValidator{
private Map<String, ValidationBean> validationMap;
public SeniorStudentValidator(Map<String,ValidationBean> validationMap){
this.validationMap = validationMap;
}
public void validate(Student student) throws StudentValidationException{
// make use of validation map for apply senior student related validations on the student
}
}
私の質問は、StudentValidatorFactory.createStudentValidator(int studentType)メソッドについて、永続ストレージからの検証マップの読み取り (学生タイプに基づく) を create メソッド内で行う必要があるかどうかです。そうでなければ、ファクトリはそのような実装の詳細について認識/依存する必要がありますか?
学生バリデータを作成するときに switch(studentType) ステートメントを回避する解決策があれば幸いです。私の頭の上のアイデアは、内部で管理されたマップを持ち、リフレクションを介して StudentValidator 具象クラスのインスタンス化を実行することです。
このような手法を使用する利点は、バリデーターのテストが (依存性注入によって) はるかに簡単になることです。