lundi 30 novembre 2015

How to Replace a Long Switch Statement

Let's say I have a switch statement that looks like this:

MakeADecision(enum Option, object HandleThis) {
    switch(Option)
        case 1:
            callMethodA(HandleThis);
        case 2:
            callMethodB(HandleThis);
        case 3:
            callMethodC(HandleThis);
}

Each of the methods do something totally different and can't be consolidated in anyway. Meaning, it won't work to have a single method name with some overloads, etc. callMethodA() might be a call to an external web service, while callMethodB() may just log the object in our own database. Also, this switch statement WILL grow in the future, and I'd rather not have to keep adding cases each time we need to expand.

I'm thinking there must be a pattern to handle this, just not sure what it is. Any ideas?

Aucun commentaire:

Enregistrer un commentaire