Factory Method VS Factory Object

2.8k views Asked by At

As I understand Factory Method is Simple Factory and Factory Object is Abstract Factory? And:

-Factory Method (Simple Factory):

public class SimplePizzaFactory {
    public static final int CHEESE = 1;
    public static final int PEPPERONI = 2;
    public static final int VEGGIE = 3;

    public static Pizza createPizza(int type) {
        Pizza pizza = null;

        if (type == CHEESE) {
            pizza = new CheesePizza();
        } else if (type == PEPPERONI ) {
            pizza = new PepperoniPizza();
        } else if (type == VEGGIE ) {
            pizza = new VeggiePizza();
        }

        return pizza;
    }
}

Factory Object(Abstract Factory):

?

Am I right?

How much are there realizations of Factory patterns and what is their difference?

1

There are 1 answers

4
Bozho On BEST ANSWER

No. A factory-method is a factory that does not require any state. A factory class is a class itself - it has state, and methods that alter that state. In the end you call the .create() method, and it uses its current state to create a new object of a different type.

Abstract factory is a different thing - there you have multiple factory implementations of the same abstract concept. The wikipedia example is about e GUIFactory - this is an abstract factory, which has two implementations: WinFactory and OSXFactory. The client code does not know which implementation it is using - it just knows the factory creates Button instances. Which make it possible to write the same code regardless of the OS.