mercredi 29 novembre 2017

Java: avoid spaguetti code

I often so far it comes me up the next code:

public ClassA getClassA(AType type)
{
    switch (type)
    {
        case AType.T1:
           //result = a lot of code
           break;
        case AType.T2:
           //result = a lot of code
           break;
        case AType.T3:
           //...
    }

    return result;
}

I'm posting this question here because I looking for a way to do it a bit more ellegant using design patterns (creating additional classes (strategies?, factories?...) or whatever idea.

Currently, I think this code is like an spaguetty-like code.

Any ideas?

Aucun commentaire:

Enregistrer un commentaire