英文:
Design pattern to read a record and determine what type of record it is?
问题
例如,一个CSV文件包含信用卡记录,我想要确定它是什么类型的信用卡?我对策略模式和模板设计模式感到困惑。
英文:
For example a csv file contains credit card records and I want to determine what type of credit card it is? I am confused between Strategy and Template Design Patterns
答案1
得分: 0
信用卡类型相当有限,只有那么多种标准信用卡。如果您想要确定它是哪种类型的信用卡,一种方法是使用一个接口来表示信用卡,然后使用枚举来表示有限数量的可能卡片。
public interface CreditCard {
}
enum StandardCreditCard implements CreditCard {
MASTERCARD,
AMERICAN_EXPRESS,
BANK_OF_AMERICA
}
这种方法的优点是快速且简便。枚举默认使用多例模式。每个元素都是 CreditCard 类型,但是如果将来需要,您仍然可以添加更多的信用卡。
interface MyOtherCreditCard extends CreditCard {
}
英文:
Credit card types are fairly limited, there are only so many standard credit cards. If you're trying to determine what type of credit card it is, one approach would be to use an interface to represent the credit card and then a enum to represent the likely finite number of cards.
public interface CreditCard {
}
enum StandardCreditCard implements CreditCard {
MASTERCARD,
AMERICAN_EXPRESS,
BANK_OF_AMERICA
}
The positives to this approach is that it's quick and dirty. Enums by default use the multition pattern. Each element is of type CreditCard, but you can still add more credit cards in the future if you need to.
interface MyOtherCreditCard extends CreditCard {
}
专注分享java语言的经验与见解,让所有开发者获益!
评论