Design patterns 您是否会在这里实施战略模式?如果是,如何实施?

Design patterns 您是否会在这里实施战略模式?如果是,如何实施?,design-patterns,oop,Design Patterns,Oop,考虑一个生成生产计划的应用程序(下面的简化代码示例)。有一个很大的产品列表,在生产计划中进行复杂计算时,我们多次调用product.GetProductionTime()。我们需要product.GetProductionTime()根据我们正在使用的规划算法或我们所处的算法步骤做出不同的行为。GetProductionTime()中的条件很难看,添加另一个算法并不容易 我在考虑战略模式。这是实施它的好地方吗?如果是,您将如何实施?如果没有,我能做什么 public class Producti

考虑一个生成生产计划的应用程序(下面的简化代码示例)。有一个很大的产品列表,在生产计划中进行复杂计算时,我们多次调用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;
    }
}
公共类生产计划问题
{
公开上市产品;
公共void GenerateFastProdPlan()
{
foreach(产品中的产品)
{
//做大量的计算
product.GetProductionTime(PlanType.Fast);
//做大量的计算
}
}
public void GenerateSlowProdPlan()
{
foreach(产品中的产品)
{
//做大量的计算
product.GetProductionTime(PlanType.Slow);
//做大量的计算
}
}
}
公共类产品
{
public int GetProductionTime(Plantype Plantype)
{
if(plantype.Fast)
返回CalculationFast();
if(plantype.Slow&&SomeOtherConditionsHold)
返回计算流();
返回CalculationFast();
}
private int CalculationFast()
{
//根据产品的多个领域进行快速计算
返回结果;
}
私有int计算流()
{
//根据产品的许多领域进行缓慢但更精确的计算
返回结果;
}
}

基本上,您希望在GetProductionTime中删除那个大的if/switch语句,并将每个实例转换成各种更小、合理的类。每个类将使用不同的条件调用CalculationFast或CalculationSlow的不同策略

例如,如果您的语言支持内部类(Java),PlantType只需检查产品的状态即可决定快与慢:

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
{
public int Calc();
}
公共类产品
{
公共类Plantype\u实现Plantype
{
公共整数计算()
{
如果(){
返回CalculationFast();
}否则{
返回计算流();
}
}
}
公共类Plantype\u两个实现Plantype
{
公共整数计算()
{
if(的一些不同且简单的条件成立){
返回CalculationFast();
}否则{
返回计算流();
}
}
}
//等等。
public int GetProductionTime(Plantype Plantype)
{
返回plantype.Calc();
}
private int CalculationFast()
{
//根据产品的多个领域进行快速计算
返回结果;
}
私有int计算流()
{
//根据产品的许多领域进行缓慢但更精确的计算
返回结果;
}
}
基本上,您的算法可以选择在给定点上有意义的Plantype类型,并将其传递给GetProductionTime


内部类方法可能是完全错误的,这取决于Plantype类需要检查什么,但您已经了解了情况。

可以使用策略模式进行检查。我建议您执行以下操作: 在Product类中创建一个接口,用于计算生产时间。 然后实现一个策略类ProdTimeCalculationStrategyBase,该类将具有虚拟GetProductTime方法并从中继承所有其他策略类。在每一个策略中,你都能实现它自己的计算方法

之后,实施一个特殊的工厂,您的开关将移动。此工厂将根据您将向其提供的产品创建策略计算类的实例


之后,您的代码将如下所示:当要求产品计算ProductionTime时,它将向工厂提供所有详细信息,以创建用于计算的特殊策略。工厂将返回能够以正确方式计算的对象。从strategy返回的结果将被提供给product,它将返回给调用者。

如果strategy模式删除了代码重复,您只需要使用它。您要删除的副本在哪里?如果是条件语句,策略模式只会增加复杂性,而不会解决问题。

plz提供了一个更好的例子来解释它