OOP-Design for validating user input

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.

+4
source share
3 answers

The validator in your example has no state (and does not need it), but another validator may require one (say, in the format):

Example:

public class RegExValidator {
    private Pattern pattern;

    public RegExValidator(String re) {
        pattern = Pattern.compile(re);
    }

    public void validate(String input) throws ValidationException {
        if (!pattern.matcher(input).matches()) {
            throw new ValidationException("Invalid syntax [" + input + "]");
        }
    }
}
+1
source

( , ):

?

interface Validator<T> {
    void validate(T toValidate) throws ValidationException;
}

, T . :

class EmptyStringValidator implements Validator<String> {
    public void validate(String toValidate) {
        if(toValidate == null || toValidate.isEmpty()) throw new ValidationException("empty!!!");
    }
}

. , Java 8, , :

class ValidationUtil {
    public static void emptyString(String val) // same code as above
}

ValidationUtil::emptyString Validator<String>. .

, ...

class ListIsSortedValidator implements Validator<Integer> {
    private int lastInt = Integer.MIN_VALUE;
    public void validate(Integer val) throw ValidationException {
        if (val < lastInt) throw new ValidationException("not sorted");
        lastInt = val;
    }
}

, , :

List<Integer> list = createList();
Validator<Integer> validator = new ListIsSortedValidator();
list.forEach(validator::validate);
+1

, , , . -.

, Validator , , , , , -. Validator ( EmptyStringValidator) . , - ( ).

, -, . , . RegistrationForm . " ", , , .

- , "" .

public final class RegistrationForm extends Form {
    ...
    @Override
    public void submit() {
        // Do validation here
        // Set input fields to error if there are problems
        // If everything ok do logic
    }
}

, , -. - .

, :

  • , - . UI-, .
  • OO , , . () , Validator ( ).
-1

Source: https://habr.com/ru/post/1691922/


All Articles