Question

I have read about Factory Method where Sub class creates needed Object and Abstract Factory has methods where concrete classes creates needed Object

Factory Method

public class PizzaStore {

    public Pizza orderPizza(String type) {
        Pizza pizza = createPizza(type);
        pizza.prepare();
        pizza.bake();
        pizza.cut();
    }

    abstract Pizza createPizza(String type);

}

public NewYorkPizzaStore extends PizzaStore {

    public Pizza createPizza(String type) {
        Pizza pizza = null;
        if("cheese".equals(type)) {
            pizza = new CheesePizza();
        }
        else if("onion".equals(type)) {
            pizza = new OnionPizza();
        }

        return pizza;
    }

}

public class pizzaTestDriveByFactoryMethod() {

    public static void main(String args[]) {
        PizzaStore ps =  new NewYorkPizzaStore();
        ps.orderPizza("cheese");
    }

}

Using a Factory

public class NewYorkPizzaFactory extends PizzaFactory {

    public Pizza createPizza(String pizza) {
        Pizza pizza = null;
        if("cheese".equals(type)) {
            pizza = new CheesePizza();
        } else if("onion".equals(type)) {
            pizza = new OnionPizza();
        }

        return pizza;
    }

}

public class PizzaStore {

    PizzaFactory factory;

    public PizzaStore(PizzaFactory factory) {
        this.factory =  factory
    }

    public Pizza orderPizza(String type) {
        Pizza pizza =  factory.createPizza(type)
        pizza.prepare();
        pizza.bake();
        pizza.cut();
        return pizza;
    }

}

public class pizzaTestDriveByAbstractFactory() {

    public static void main(String args[]) {
        PizzaFactory nwFactory = new NewYorkPizzaFactory();
        PizzaStore ps =  new PizzaStore(nwFactory);
        ps.orderPizza("cheese");
    }

}

Same use case implemented using Factory Method and Abstract Factory. Why there should be a FactoryMethod instead of using Abstract Factory or a Utility Factory (Such as Chicago Factory/NewYorkFactory). In which case Factory method is useful on Abstract Method?

Was it helpful?

Solution 2

You should ask whether the factory task is conceptually tied to the base class and/or subclasses (like createPizza) or not (like procurePizzaBoxes). The pizza box supplier is not only conceptually distinct, but can be swapped. It might even be one national company supplying pizza boxes to every city.

Another way to do decide is that if you're making subclasses just to implement the factory method, then you'd be better off factoring it out into an abstract factory.

But if the subclasses have their own characteristics and need to exist regardless, and the implementation of the method is tied to them (even if it's procurePizzaBoxes, but the supplier is local and isn't an important detail), then you should use factory method as a natural application of polymorphism and OO, and to keep the class count down.

Refactoring from factory method to abstract factory is probably easier than the reverse, and requires fewer classes, so can be considered the more conservative choice.

OTHER TIPS

The main difference is that you can implement a factory object without needing to sub class the object you are processing the factory too. This also means that you can do things like swap factories in the fly. On the other hand if you are just doing something simple or closely coupled then you might as well just provide the method as that is simpler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top