设计模式,用于读取记录并确定其类型是什么?

huangapple 未分类评论43阅读模式
英文:

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 {

}

huangapple
  • 本文由 发表于 2020年4月10日 04:14:13
  • 转载请务必保留本文链接:https://java.coder-hub.com/61129491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定