mardi 28 novembre 2017

Purpose of passing factory object in Simple Factory Design Pattern (Head-First)?

I am from C++ background, recently started learning Design Patterns.

I am facing problems with this code from Head First Design Patterns:

Link: PizzaStore.java

public class PizzaStore {
SimplePizzaFactory factory;

public PizzaStore(SimplePizzaFactory factory) { 
    this.factory = factory;
}

public Pizza orderPizza(String type) {
    Pizza pizza;

    pizza = factory.createPizza(type);

    pizza.prepare();
    pizza.bake();
    pizza.cut();
    pizza.box();

    return pizza;
} 
}

Please help me with the following doubt:

What is the relevance of passing a factory object in the Constructor of PizzaStore class ?

  • PizzaStore class already contains a SimplePizzaFactory object
  • The passed-on object is not initialized with any data (which needs to be copied by PizzaStore Constructor):

    public PizzaStore(SimplePizzaFactory factory) { 
    this.factory = factory;
    
    

    }

Thanks

Aucun commentaire:

Enregistrer un commentaire