生産計画を作成するアプリケーション(以下の簡単なコードサンプル)を検討してください。プロダクトの大きなリストがあり、プロダクションプランで複雑な計算をしながらproduct.GetProductionTime()を何度も呼び出します。 GetProductionTime()の条件は、醜いですし、別のアルゴリズムを追加することは容易ではありません。ここに戦略パターンを実装しますか?
私は戦略パターンを考えています。これはそれを実装するのに適した場所ですか?はいの場合、それをどのように実装しますか?いいえ、私は何ができますか?
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;
}
}
plzはそれをよりよく説明するための例を提供しています –