samedi 28 juillet 2018

How to apply design pattern to replace Switch condition code smell for below code

I have below implementation to find cardType.

public enum CardType{
CREDIT,DEBIT , LOYALTY;

}

class CardTypeChecker{
public String getCardTypeInString(CardType cardType) {
    return  convert(cardType)
}

protected String convert(CardType cardType) {
    switch (cardType) {
    case CREDIT:
        return CardType.CREDIT.name();
    case DEBIT:
        return CardType.DEBIT.name();
    case LOYALTY:
        return CardType.LOYALTY.name();
    default:
        throw new IllegalArgumentException("Provided CardType is NOT available.");
    }
}
}

Now i want to know how i can apply design pattern to remove this code smell of switch statement.

Aucun commentaire:

Enregistrer un commentaire