What are the disadvantages of Simple Factory, compared to Factory Method pattern and Abstract Factory Pattern respectively?

Viewed 1989

Head First Design Patterns describes Simple Factory as

enter image description here

public class SimplePizzaFactory {
    public Pizza createPizza(String type) {
        Pizza pizza = null;
        if (type.equals(“cheese”)) {
            pizza = new CheesePizza();
        } else if (type.equals(“pepperoni”)) {
            pizza = new PepperoniPizza();
        } else if (type.equals(“clam”)) {
            pizza = new ClamPizza();
        } else if (type.equals(“veggie”)) {
            pizza = new VeggiePizza();
        }
        return pizza;
    }
}

What are the disadvantages of Simple Factory, compared to Factory Method pattern and Abstract Factory Pattern respectively?

In Design Patterns by Gamma et al, parameterized factory methods by classes in Factory Method Pattern seem similar to Simple Factory. Is it correct that parameterized Abstract Factory would be exactly Simple Factory? Does Design Patterns mention about parameterized Abstract Factory?

2 Answers

What are the disadvantages of Simple Factory, compared to Factory Method pattern and Abstract Factory Pattern respectively?

Comparing Factory method pattern with Abstract Factory Pattern is not entirely right because they serve different purposes. Factory method is simply creating an object while Abstract Factory Pattern defines an interface through which you can create families of objects. If I remember correctly both are described in the book you're reading.

Getting back to disadvantages of Simple Factory vs Factory method: When talking about design patterns it's always a matter of which and how to apply them to your specific problem and it really depends from case to case.

Having a factory class for each object type which you want to create can add complexity to your code and every object family will have a factory class nearby, so soon your code can explode with Factory classes. This is the only disadvantage which I know of, if the pattern is applied right. Of course, on the other hand, Factory method has its disadvantages as well. When picking between the two, I always ask myself who is the client of the code and what's easier for him to use.

In Design Patterns by Gamma et al, parameterized factory methods by classes in Factory Method Pattern seem similar to Simple Factory. Is it correct that parameterized Abstract Factory would be exactly Simple Factory? Does Design Patterns mention about parameterized Abstract Factory?

Not really, but concrete factory implementations of the AbstractFactory will indeed look like Simple Factories.

I recommend you also going through other articles, my personal preference is Zoran's posts and lectures on the abstract factory. See a sample here: http://codinghelmet.com/articles/cascading-abstract-factories

simple factory is having a factory class for a class that has one creation method with a large conditional that based on method parameters chooses which product class to instantiate (simple factory is not design pattern).

example of simple factory:

class UserFactory {
public static function create($type) {
    switch ($type) {
        case 'user': return new User();
        case 'customer': return new Customer();
        case 'admin': return new Admin();
        default:
            throw new Exception('Wrong user type passed.');
    }
}

}

Related