jeudi 25 août 2016

Design pattern for many if statement and Boolean condition?

I am wondering if there is any design pattern for such situation:

private static boolean check(String s) {
    if(isBook(s)) {
        System.out.println("Book");
        return false;
    }else if(isEmail(s)) {
        System.out.println("Email");
        return false;
    }
    return true;
}

private static boolean isBook(String s) {
    if(s.equals("B")) return true;
    return false;
}

private static boolean isEmail(String s) {
    if(s.equals("E")) return true;
    if(s.length() > 4) return true;
    return false;
}

There will be many isXXX in check method, but I don't wanna to have many if-else statement.

Aucun commentaire:

Enregistrer un commentaire