2009-09-03 45 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