I would suggest that you do not introduce validation logic inside the classes that you are going to validate.
I believe that it is better to save these classes as simple value objects and create a parallel hierarchy of validators, approximately one for each object being checked. Alternatively, you can also create a single validator that can check all objects: however, this solution is less scalable and can violate the open-closed principle when you need to add a new object (for example, you want to deal with car rearview mirrors) .
Assuming you take the one entity : one validator validator one entity : one validator , the container validator first checks the components inside the container and then checks to see if they match.
Also consider using validator frameworks such as the Apache Commons Validator , which can save you from writing a template. However, since I don’t know what kind of complex test you should perform, I don’t know if it meets your needs.
Also, I don’t think you should worry about checking everything before it is built. Just create it and check after that: then if it violates the verification rules, you can discard it (i.e. do not save it anywhere).
source share