2009-09-03 7 views
2

生産計画を作成するアプリケーション(以下の簡単なコードサンプル)を検討してください。プロダクトの大きなリストがあり、プロダクションプランで複雑な計算をしながら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; 
    } 
} 

答えて

1

基本的には、GetProductionTimeのbig/if/switchステートメントを取り除き、各ケースを様々な小さく分かりやすいクラスに変えたいとします。各クラスは、異なる条件を使用してCalculationFastまたはCalculationSlowを呼び出す異なる戦略になります。例えば

、あなたの言語は、内部クラス(Javaの)をサポートし、Plantypeだけ遅い速い&間で決定するために製品の状態を検査する必要がある場合:

public interface Plantype 
{ 
    public int Calc(); 
} 

public class Product 
{ 
    public class Plantype_one implements Plantype 
    { 
     public int Calc() 
     { 
      if (<some simple condition holds for Product instance>) { 
       return CalculationFast(); 
      } else { 
       return CalculationSlow(); 
      } 
     } 
    } 
    public class Plantype_two implements Plantype 
    { 
     public int Calc() 
     { 
      if (< some different & simple condition holds for Product instance >) { 
       return CalculationFast(); 
      } else { 
       return CalculationSlow(); 
      } 
     } 
    } 

    // etc. 

    public int GetProductionTime(Plantype plantype) 
    { 
     return plantype.Calc(); 
    } 

    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; 
    } 
} 

基本的には、あなたのアルゴリズムはどのような種類を選択し、選択することができますPlantypeの特定の点に意味を持ち、それをGetProductionTimeに渡します。

内部クラスのアプローチは、Plantypeクラスが検査する必要があるものに応じて完全に間違っているかもしれませんが、あなたはその画像を取得します。

0

戦略パターンで行うことができます。私はあなたに次の実装をお勧めします: プロダクションクラスを作成し、生産時間を計算するようにします。 次に、仮想GetProductTimeメソッドを持ち、他のすべての戦略クラスを継承する戦略クラスProdTimeCalculationStrategyBaseを実装します。すべての戦略の中で、あなたはそれ自身の計算方法を実装することができます。

その後、スイッチが移動する特別な工場を実装します。この工場は、あなたが提供する製品に基づいて戦略計算クラスのインスタンスを作成します。

このコードは、次のように動作します。製品にProductionTimeを計算するように求められたら、すべての詳細を工場に提供して計算のための特別な戦略を作成します。ファクトリは正しい方法で計算できるオブジェクトを返します。戦略から返された結果が製品に与えられ、呼び出し元に返されます。

0

コードの複製を削除する場合にのみ、戦略パターンを使用することができます。削除したい複製はどこにありますか?条件文の場合、戦略パターンは問題を修正せずに複雑さを増やします。

+0

plzはそれをよりよく説明するための例を提供しています –

関連する問題