Consider an application that generates production plans (simplified code sample below) . There's a big list of products and we call product.GetProductionTime() many times while doing complex calculations in the production plan. We need product.GetProductionTime() to behave differently based on the planning algorithm we are using or the step of the algorithm that we are in. The conditionals in the GetProductionTime() is ugly and adding another algorithm is not easy.
I am thinking about strategy pattern. Is this a good place to implement it? If yes, how would you implement it? If no, what can I do?
public class ProductionPlanningProblem
{
public List<Product> Products;
public void GenerateFastProdPlan()
{
foreach (Product product in Products)
{
//do lots of calculations
product.GetProductionTime(PlanType.Fast);
//do lots of calculations
}
}
public void GenerateSlowProdPlan()
{
foreach (Product product in Products)
{
//do lots of calculations
product.GetProductionTime(PlanType.Slow);
//do lots of calculations
}
}
}
public class Product
{
public int GetProductionTime(Plantype plantype)
{
if(plantype.Fast)
return CalculationFast();
if (plantype.Slow && SomeOtherConditionsHold)
return CalculationSlow();
return CalculationFast();
}
private int CalculationFast()
{
//do fast calculation depending on many fields of product
return result;
}
private int CalculationSlow()
{
//do slow but more accurate calculations depending on many fields of product
return result;
}
}