我在下面的实现中找到了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.");
}
}
}
现在我想知道我如何应用设计模式来消除switch语句的代码。
发布于 2018-07-31 11:45:06
试试这个样本,虽然它是在c#中,但它应该给出要点。
public enum CardType{
CREDIT,DEBIT , LOYALTY;
}
class CardTypeChecker
{
Dictionary<CardType, string> dictionary = new Dictionary<CardType, string>();
public CardTypeChecker()
{
dictionary.Add(CardType.CREDIT, CardType.CREDIT.ToString());
dictionary.Add(CardType.DEBIT, CardType.DEBIT.ToString());
dictionary.Add(CardType.LOYALTY, CardType.LOYALTY.ToString());
}
public String getCardTypeInString(CardType cardType)
{
return dictionary[cardType];
}
}
https://stackoverflow.com/questions/-100001777
复制相似问题