I’ve got two validator classes. Let’s say: Car Validator and Wheel validator, and the second one is enclosed in first.
CarBodyValidator.validate(){
WheelValidator.validate();
}
I have more validators in CarBodyValidator, f.e EngineValidator etc. validate() should only return true or false, and at one time I want to print as many mistakes as possible (if validation returns false for some class, I want to validate other classes if they are not dependent on each other).
Now, let’s say that Wheel validator returns a useful information, that I need in the CarBody validator. F.e if wheel diameter is too big then I need that info in car validator to validate something else.
How do I pass that information. I could create a field in Wheel validator, that stores the parameter, but accessing it like that:
carBodyValidator.validate(){
wheelValidator.validate();
if (WheelValidator.getSmomething() == somethingElse){
...
}
}
seems wrong (I mean validator should only validate and not do anything else). Is this the correct way or I should do it different.
In situations like this I often rely on the observer pattern. Please note this is all pseudo code.
Create a ValidatorObserver interface.
Make CarBodyValidator implement this interface.
Create a register method on WheelValidator and setup notification
Somewhere in your code you will need to register the observer