vendredi 25 septembre 2015

Refactoring conditionals

I've to output a text based on some conditionals, how can I refactor this to make it clear to understand and maintenance?

If the best option is replace with state, I'll need to create a class for each combinations of enums?

public enum CalcType {A, B, C, D}
public enum LicensingOption {HOME, PRO, ULTIMATE}

public void printHeader() {
    switch (calc) {
        case A:
            printHeaderX();
            break;
        case B:
            printHeaderY();
            break;
        default:
            printHeaderByLicensingOption();
    }
}

public void printHeaderByLicensingOption() {
    switch (license) {
        case PRO:
            printHeaderW();
            break;
        case HOME:
            printHeaderZ();
            break;
        case ULTIMATE:
            printHeaderA();
            break;
    }
}

public void printFooter() {
    if (calc.equals(CalcType.A))
        printFooterX();
    else
        printFooterByLicensingOption();
}

public void printFooterByLicensingOption() {
    switch (license){
        case PRO:
            printFooterW();
            break;
        case HOME:
            printFooterZ();
            break;
        case ULTIMATE:
            printFooterA();
            break;
    }
}

public void printFooterW(){
    if (calc.equals(CalcType.B))
        printW1();
    else
        printW2();
}

Aucun commentaire:

Enregistrer un commentaire