Java - validator design, class hierarchy

I am working on creating a validator for certain objects (fields of these objects). These objects are enclosed in one larger object - a container.

Example: a car as a container. Consists of wheels, engine, body. Suppose I need to check if the wheels have the right diameter, the engine has the right power, the body has a certain length, etc.

Theoretically, I think that I should check everything before creating a container (car).

What is the best way to achieve this? Can I create an abstract validator class using the validate () method and implement it in every private class? What about the container, I just do not include it at all in the verification process? Thanks for the help.

+4
source share
3 answers

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).

+2
source

You can create the ValidatablePart interface using the verification method, all parts implement this interface, and then the container checks all the closed parts as they are added to the container, or, possibly, by invoking the container assembly or any other method that is supposed to build it.

Your Container class can follow pattern template pattern .

+1
source

Answers to answer questions gd1, I agree. One such way would be to have a ValidatorAdapter for each of your value objects. So it will look like this:

 public class GreenCarValidator { public GreenCarValidator(Car car) { // save reference } @Override public boolean isValid() { return car.getColor().equals("green"); } } public class RedCarValidator { public RedCarValidator(Car car) { // save reference } @Override public boolean isValid() { // you could compose more validators here for each property in the car object as needed return car.getColor().equals("red"); } } 

You can now have many types of validators for one type of object, dynamic and custom at runtime. If you put the valid () method inside classes, then classes like gd1 suggest that you don’t do this, you will lose this flexibility.

+1
source

All Articles