I need to make an extension point for custom validation rules in java. Can you help me to find the best way to do it?
My code looks like this
...
public myClass() {
...
//some business logic
customValidator.validate(event); //custom validator
...
}
I know that it is suitable to use base Validator
interface and implement it with custom validators.
interface Validator {
public static Boolean validate();
}
public class TrueValidator implements Validator {
public static Boolean validate() { return true;}
}
public class TrueValidator implements Validator {
public static Boolean validate() { return true;}
}
I want to know what is the best pattern of calling validation depending on some String
variable. Is it okay just to get class with Reflection API? I will get my String
from database, create Class
and create instance of it.
The other solution I know is to make Validator
factory and get Validator
by String
variable, but I think that it is too excessive.
Can you recommend me somethink?
Aucun commentaire:
Enregistrer un commentaire