Is there a design pattern that lets me more efficiently instantiate my concrete classes, without having to rely on an if statement?
Suppose I have an inheritance set up like this:
public abstract class Asset
{
public string Id {get; set;}
}
public interface ICalculable
{
void Calculate();
}
public class Equity : Asset, ICalculable
{
public decimal Price {get; set;}
public void Calculate()
{
//...
}
}
public class Options : Asset, ICalculable
{
public int StrikePrice {get; set;}
public void Calculate()
{
//...
}
}
Then, I'm reading from rows of data and constructing my concrete classes:
List<Asset> listOfAssets = new List<Asset>();
for(var row in rows)
{
Asset asset;
if(row.AssetType = "Equity")
asset = new Equity(row.CUSIP);
else
asset = new Options(row.strike_price);
listOfAssets.Add(asset);
}
Aucun commentaire:
Enregistrer un commentaire