Reputation: 133
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 ?
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
Upvotes: 1
Views: 212
Reputation: 1548
In Java "everything is an object" and needs to be instantiated explicitly, that's why you can think of the SimplePizzaFactory
inside the PizzaStore
class as a pointer that needs to be instantiated (needs to have the new SimplePizzaFactory();
) the same with the argument passed into the constructor, imagine a c++ pointer passed as an argument to the constructor, the pointer should already be instantiated somewhere else prior to calling the PizzaStore
's constructor.
Upvotes: 0
Reputation: 2099
Have a look at PizzaTestDrive class where the PizzaStore
gets its SimplePizzaFactory
.
SimplePizzaFactory factory = new SimplePizzaFactory();
PizzaStore store = new PizzaStore(factory);
This is where the actual SimplePizzaFactory
is created and passed on to the constructor. If you do not do this, then the factory will not be initialized, i.e. null
and the following line of code will throw a NullPointerException
:
pizza = factory.createPizza(type);
Upvotes: 0
Reputation: 12463
This is one place where Java differs from C++. When you type
SimplePizzaFactory factory;
It's not like in C++ where this implicitly calls a no-argument constructor and creates an object. In Java, factory
will simply be null
. This is because variables in Java are more similar to pointers in C++. The above is similar to
SimplePizzaFactory *factory;
You'll recognize that this doesn't create a value at all. It just makes a pointer that does not point to anything useful. If you wanted to give it a value, you would have to do
factory = new SimpleFactory(...);
or, if you have another pointer, you can pass that pointer's value to it, so it will point to the same value.
SimpleFactory *otherFactory = ...;
...
factory = otherFactory;
That's how it works in Java. In short, Java variables are just references to objects, just like C++ pointers (but with garbage collection). To use a reference, first you need to assign it a value.
Upvotes: 2
Reputation: 522
PizzaStore class already contains a SimplePizzaFactory object
Well, PizzaStore have a non-initialized attribute of type SimplePizzaFactory. In the PizzaStore constructor, that attribute is initialized with a reference to the constructor argument. This is an usual pattern in Java code.
The passed-on object is not initialized with any data (which needs to be copied by PizzaStore Constructor)
There is no need to add initialization code in the constructor, is asumed that the SimplePizzaFactory is already initialized. That factory is not copied, is referenced by the PizzaStore attribute, so the factory inside the PizzaStore, after the constructor is executed, is an already initialized object.
Upvotes: 3