工厂方法模式以避免基于条件逻辑的对象实例化

发布于 2024-09-10 17:06:31 字数 842 浏览 3 评论 0原文

在如下场景中,需要根据某些条件逻辑来实例化对象,工厂方法模式是否可以帮助避免客户端代码由于 if/elseif 条件的数量而变得混乱(这也可能是如果由于逻辑的不同变化而需要创建越来越多的产品,这将是维护的噩梦)。

或者还有其他设计模式可以拯救吗?

public interface IProduct
{
    void Method1();
}

public class ProductA : IProduct
{
    void Method1()
    {
    }
}

public class ProductB : IProduct
{
    void Method1()
    {
    }
}

public class ProductC : IProduct
{
    void Method1()
    {
    }
}

public class Client
{
    public void Test()
    {
        int count = 5;

        IProduct product;

        if (count < 10)
        {
            product = new ProductA();
        }
        else if (count == 10)
        {
            product = new ProductB();
        }
        else if (count > 10)
        {
            product = new ProductC();
        }

        product.Method1();


    }
}

In a scenario like below where an object needs to be intantiated based on some conditional logic, can the factory method pattern help to avoid client code getting cluttered due to number of if/elseif conditions (which would also be a maintenance nightmare if more and more products needs to get created due to different variations of logics).

Or else is there a any other design pattern that could come to rescue?

public interface IProduct
{
    void Method1();
}

public class ProductA : IProduct
{
    void Method1()
    {
    }
}

public class ProductB : IProduct
{
    void Method1()
    {
    }
}

public class ProductC : IProduct
{
    void Method1()
    {
    }
}

public class Client
{
    public void Test()
    {
        int count = 5;

        IProduct product;

        if (count < 10)
        {
            product = new ProductA();
        }
        else if (count == 10)
        {
            product = new ProductB();
        }
        else if (count > 10)
        {
            product = new ProductC();
        }

        product.Method1();


    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘦怹 2024-09-17 17:06:31

看起来工厂模式正是您想要的:


public class ProductFactory
{
    public IProduct GetProduct(int count)
    {
        if (count < 10)
        {
            return new ProductA();
        }
        else if (count == 10)
        {
            return new ProductB();
        }
        else if (count > 10)
        {
            return new ProductC();
        }
    }
}

然后无论您的客户端代码需要一个实例,它都可以去


public class Client
{
    public void Test()
    {
        ProductFactory factory = new ProductFactory();

        IProduct product = factory.GetProduct(5);

        product.Method1();
    }
}

It looks like the factory pattern is exactly what you want:


public class ProductFactory
{
    public IProduct GetProduct(int count)
    {
        if (count < 10)
        {
            return new ProductA();
        }
        else if (count == 10)
        {
            return new ProductB();
        }
        else if (count > 10)
        {
            return new ProductC();
        }
    }
}

Then wherever your client code needs an instance it can just go


public class Client
{
    public void Test()
    {
        ProductFactory factory = new ProductFactory();

        IProduct product = factory.GetProduct(5);

        product.Method1();
    }
}

明月松间行 2024-09-17 17:06:31

我不相信Carver先生的解决方案严格遵循GOF设计模式书中描述的工厂模式的结构。然而,它是一种非常常见的编程习惯(简单工厂),并且可能是这种设计的好方法。

工厂模式要求从创建者派生具体创建者。通常,创建者将产品的创建委托给具体创建者。当将来可能添加新工厂时,这非常有用。例如,想象一下,如果您创建了一个新工厂,可以使用较少的资源(计数用作“资源”的代理)来制造特定产品。

    public interface IProduct
    {
      void PerformService();
    }

    public class ProductA : IProduct
    {
      public void PerformService()
      {
        Console.WriteLine("Product A's service.");
      }
    }

    public class ProductB : IProduct
    {
      public void PerformService()
      {
        Console.WriteLine("Product B's service.");
      }
    }

    public class ProductC : IProduct
    {
      public void PerformService()
      {
        Console.WriteLine("Product C's service.");
      }
    }

    abstract class ProductFactory
    {
      public abstract IProduct CreateProduct(int count);
    }

    class OriginalFactory : ProductFactory
    {
      public override IProduct CreateProduct(int count)
      {
        if (count < 10)
        {
          return new ProductA();
        }
        else if (count == 10)
        {
          return new ProductB();
        }
        else if (count > 10)
        {
          return new ProductC();
        }
        else
        {
          return null;
        }
      }
    }

    class NewFactory : ProductFactory
    {
      public override IProduct CreateProduct(int count)
      {
        if (count < 20)
        {
          return new ProductA();
        }
        else if (count == 20)
        {
          return new ProductB();
        }
        else if (count > 20)
        {
          return new ProductC();
        }
        else
        {
          return null;
        }
      }
    }

    public class FactoryTest
    {
      public void TestNew()
      {
        ProductFactory factory = new NewFactory();
        IProduct product = factory.CreateProduct(10); // Product B
        product.PerformService();
      }
      public void TestOld()
      {
        ProductFactory factory = new OriginalFactory();
        IProduct product = factory.CreateProduct(10); // Product A
        product.PerformService();
      }
    }

I don't believe Mr. Carver's solution strictly follows the structure of the Factory Pattern described in the GOF Design Pattern book . However, it is a very common programming idiom (Simple Factory) and likely a good approach for this design.

The Factory Pattern calls for the Concrete Creator to be derived from a Creator. Typically, the Creator delegates the creation of the product to the Concrete Creator. This is useful when a new factory might be added in the future. For example, imagine if you created a new factory that could manufacture a the specific products with less resources (count is used as a proxy for "resource").

    public interface IProduct
    {
      void PerformService();
    }

    public class ProductA : IProduct
    {
      public void PerformService()
      {
        Console.WriteLine("Product A's service.");
      }
    }

    public class ProductB : IProduct
    {
      public void PerformService()
      {
        Console.WriteLine("Product B's service.");
      }
    }

    public class ProductC : IProduct
    {
      public void PerformService()
      {
        Console.WriteLine("Product C's service.");
      }
    }

    abstract class ProductFactory
    {
      public abstract IProduct CreateProduct(int count);
    }

    class OriginalFactory : ProductFactory
    {
      public override IProduct CreateProduct(int count)
      {
        if (count < 10)
        {
          return new ProductA();
        }
        else if (count == 10)
        {
          return new ProductB();
        }
        else if (count > 10)
        {
          return new ProductC();
        }
        else
        {
          return null;
        }
      }
    }

    class NewFactory : ProductFactory
    {
      public override IProduct CreateProduct(int count)
      {
        if (count < 20)
        {
          return new ProductA();
        }
        else if (count == 20)
        {
          return new ProductB();
        }
        else if (count > 20)
        {
          return new ProductC();
        }
        else
        {
          return null;
        }
      }
    }

    public class FactoryTest
    {
      public void TestNew()
      {
        ProductFactory factory = new NewFactory();
        IProduct product = factory.CreateProduct(10); // Product B
        product.PerformService();
      }
      public void TestOld()
      {
        ProductFactory factory = new OriginalFactory();
        IProduct product = factory.CreateProduct(10); // Product A
        product.PerformService();
      }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文