2009-09-03 51 views
2

考慮一個生成生產計劃的應用程序(下面的簡化代碼示例)。有很多產品清單,我們多次調用product.GetProductionTime(),同時在生產計劃中進行復雜的計算。我們需要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/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; 
    } 
} 

基本上,你的算法可以挑選什麼樣的植物類型在給定點是有意義的,並將其傳遞給GetProductionTime。

內部類方法可能是完全錯誤的,這取決於Plantype類需要檢查的內容,但是您可以獲取圖片。

0

可以用戰略模式來做到這一點。我建議你下面的實現: 在Product類中創建一個接口,它將允許計算生產時間。 然後實現一個策略類ProdTimeCalculationStrategyBase,該類將具有虛擬GetProductTime方法並從中繼承所有其他策略類。在每一種策略中,你都可以實現它自己的計算方法。

之後,實現一個特殊的工廠,您的交換機將移動。該工廠將根據您將提供給它的產品創建一個戰略計算類的實例。

之後,您的代碼將如下操作:當產品被要求計算ProductionTime時,它會提供所有詳細信息給工廠以創建特殊的計算策略。工廠將以正確的方式返回能夠計算它的對象。從戰略返回的結果將被賦予產品,並將其返回給調用者。

0

如果刪除代碼重複,您只想使用策略模式。你想刪除的重複在哪裏?如果是條件陳述,戰略模式只會增加複雜性而不解決問題。

+0

PLZ提供了一個例子來解釋它更好 – 2012-10-26 13:32:25