I have a design problem.
I have two data objects which are instances of say class A and class B.
A and B don’t have any behavior – they are java beans with getters and setters.
I have a Validation interface and 10 implementations of it defining different Validations.
I would like to specify in my properties file which Validation applies to which class.
Something like this:
class A XYZValidation,ABCValidation
class B: ABCValidation, PPPValidation, etc
How do I write my Validation class so that it serves objects that are instances of Class A OR ClassB, or just about any other Class C that I might want to add in future?
interface Validation {
public boolean check(??);
}
> Just wanted to add this line to say thank you to all those who have responded to this post and to say that I am loving my time here on this amazing website. Stackoverflow rocks!
Have you thought about using annotations to mark the fields you want to validate in your bean?
If you have 10 different validations you could specify 10 annotations. Then mark the fields using annotations:
With reflection API iterate through all the fields and see if they are marked, something like this:
An option would be to mark the whole class with the validator you want to use.
EDIT: remember to include annotation:
for your annotation interface.
EDIT2: please don’t modify the fields directly (as in the example above). Instead access their getters and setters using reflection.