I am currently trying to develop some things based on OO principles. Therefore, let’s say, before processing user input, I need to check it. According to OO, a separate Validator class would be correct. It looks like this:
public class Validator{
public void validate(String input) throws ValidationException{
if (input.equals("")) throw new ValidationException("Input was empty");
}
}
Then my processing class, which received the validator object before introducing the dependency, will call validator.validate(input)
The good point about this design is that
- My processing class can get a mock for the validator via DI, which makes testing easier
- Validator class can be checked independently
However, my doubts are in the design of the Validator. According to O.O., he missed some state. With this construct, it's like a util class, and a validate method can be static
. And I read many times that the (static) Util classes are a bad OO design. So, how can this be done with a lot of OO, while retaining both of the advantages that I mentioned?
PS: Maybe OO is just a bad solution for this kind of problem. However, I would like to see what the OO solution looks like and form my own opinion.
source
share