vendredi 23 octobre 2015

Best way to structure with many vadidating method

Here is my code

public class Validator {
private String message = "ok";

public String mainValidate(String value) {
    if(!isAccept1()) {
        message = "fail1";
        return message;
    }

    if(!isAccept2()) {
        message = "fail2";
        return message;
    }

    if(!isAccept3()) {
        message = "fail3";
        return message;
    }
    return message;
}

public boolean isAccept1() {}

public boolean isAccept2() {}

public boolean isAccept3() {}

Requirement is: If the code meet any error, return message immediately. As you can see, with current code, i repeat myself very much. How can I structure the code and still keep requirement. If any error occur, the code skip other validate and return error message

Many thanks!

Aucun commentaire:

Enregistrer un commentaire