Lets say we have a family of classes (cards, for the sake of it), and we need to instantiate them based on some identifier.
A factory method would look like this:
public Card GetCard(int cardNumber)
{
switch(cardNumber)
{
case 13: return new King();
case 12: return new Queen();
case 11: return new Jack();
}
//...
}
What I want is to avoid this switch
. Why? Maybe I want to reuse this comparison in the feature.
What I came up with is something like this:
private Dictionary<int, Type> cardTypes =
{
{13, typeof(King)},
{12, typeof(Queen)},
{11, typeof(Jack)}
};
public Card GetCard(int cardNumber)
{
var cardType = cardTypes[cardNumber];
var instance = Activator.CreateInstance(cardType);
return (Card)instance;
}
However, this solution uses reflection which is expensive, and is also problematic when you have more than one "identifier" (for example 1 and 14 both give Ace
- should I add 2 keys to the dictionary?).
What's the best practice in this scenario?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…