samedi 9 avril 2016

Design Patterns: Builder within a Factory

I'm wondering if this is a valid practice of design patterns. I'm currently designing a code where it seems that there is a builder within the factory. I can smell something bad in my current design but I can't pinpoint it. The code looks something like this...

class Program
{
    static void Main()
    {
        string productName = "productA";
        IProduct product1 = new Factory().GetNewProduct(productName);
    }
}

class Factory
{
    internal IProduct GetNewProduct(string name)
    {
        IProduct product = null;

        switch (name)
        {
            case "productA":
                product = new ProductA();
                break;
            case "productB":
                product = new ProductB();
                break;
            case "productC":
                product = new ProductC();
                break;
            default:
                throw new Exception("Invalid product type!");
        }

        //builder (sort of)            
        product.addPart1();
        product.addPart2();
        ...

        return product;
    }
}

Aucun commentaire:

Enregistrer un commentaire