vendredi 16 juin 2017

How to best handle multiple switch cases with different overloaded methods of an interface

I have the following codes, which base on different enum values to execute different overloaded methods of an interface:

switch (type) {
            case A:
                match = dao.findBy(initiativeType, externalId);
                break;
            case B:
            case C:
            case D:
            case E:
                match = dao.findBy(initiativeType, startTime);
                break;
            case F:
            case G:
            case H:
            case J:
            case K:
            case L:
            case M:
                match = dao.findBy(externalId);
                break;
            case N:
            case O:
            case P:
            case Q:
                match = dao.findBy(initiativeType, controlledElement, startTime);
                break;
            default:
                match = dao.findBy(initiativeType, cumulativeProgramPeriodStart, controlledElement);
}

I am planning to use strategy pattern, but the problem with it is that it can't handle different method calls with different parameters.

Any suggestions for a better approach to refactor this code?

Thanks.

Aucun commentaire:

Enregistrer un commentaire