The Simple Factory isn’t actually a Design Pattern; it’s more of a programming idiom. But it is commonly used, so we’ll give it a Head First Pattern Honorable Mention. Some developers do mistake this idiom for the “Factory Pattern,” so the next time there is an awkward silence between you and another developer, you’ve got a nice topic to break the ice.
Just because Simple Factory isn’t a REAL pattern doesn’t mean we shouldn’t check out how it’s put together. Let’s take a look at the class diagram of our new Pizza Store:
Think of Simple Factory as a warm up. Next, we’ll explore two heavy duty patterns that are both factories. But don’t worry, there’s more pizza to come!
Pattern Honorable Mention
Pizza
prepare() bake() cut() box()
orderPizza()
createPizza()
CheesePizza
VeggiePizza
ClamPizza
PepperoniPizza
you are here 4 117
Head First Honorable Mention
These are our concrete products. Each product needs to implement the Pizza interface* (which in this case means
“extend the abstract Pizza class”) and be concrete. As long as that’s the case, it can be created by the factory and handed back to the client.
This is the client of the factory. PizzaStore
now goes through the SimplePizzaFactory to get instances of pizza.
*Just another reminder: in design patterns, the phrase “implement an interface” does NOT always mean “write a class the implements a Java interface, by using the “implements” keyword in the class declaration.” In
the general use of the phrase, a concrete class implementing a method from a supertype (which could be a class OR interface) is still considered to be “implementing the interface” of that supertype.
This is the factory where we create pizzas; it should be the only part of our application that refers to concrete Pizza classes..
The create method is often declared statically.
We’ve defined Pizza as an abstract class with some helpful implementations that can be overridden.
This is the product of the factory: pizza!
t
z
z
y
pizza franchise
Franchising the pizza store
Your Objectville PizzaStore has done so well that you’ve trounced the competition and now everyone wants a PizzaStore in their own neighborhood. As the franchiser, you want to ensure the quality of the franchise operations and so you want them to use your time-tested code.
But what about regional differences? Each franchise might want to offer different styles of pizzas (New York, Chicago, and California, to name a few), depending on where the franchise store is located and the tastes of the local pizza connoisseurs.
We’ve seen one approach...
If we take out SimplePizzaFactory and create three different factories, NYPizzaFactory, ChicagoPizzaFactory and CaliforniaPizzaFactory, then we can just compose the PizzaStore with the appropriate factory and a franchise is good to go. That’s one approach.
Let’s see what that would look like...
118 Chapter 4
y t
You want all the franchise pizza stores to leverage your PizzaStore code, so the pizzas are prepared in the same way.
One franchise wants a factory that makes NY style pizzas: thin crust, tasty sauce and just a little cheese.
Another franchise wants a factory that makes Chicago style pizzas; their customers like pizzas with thick crust, rich sauce, and tons of cheese.
r
r c
a
o
a
z
i
g
P
a
C
N
r
S
P
i
a
P
Y
c
e
o
z
z
o
a
F
z
F
i
h
c
i
a
o
t
But you’d like a little more quality control...
So you test marketed the SimpleFactory idea, and what you found was that the franchises were using your factory to create pizzas, but starting to employ their own home grown procedures for the rest of the process: they’d bake things a little differently, they’d forget to cut the pizza and they’d use third-party boxes.
Rethinking the problem a bit, you see that what you’d really like to do is create a framework that ties the store and the pizza creation together, yet still allows things to remain flexible.
In our early code, before the SimplePizzaFactory, we had the pizza-making code tied to the PizzaStore, but it wasn’t flexible. So, how can we have our pizza and eat it too?
I’ve been making pizza for years so I thought I’d add my own “improvements” to the PizzaStore procedures...
the factory pattern
NYPizzaFactory nyFactory = new NYPizzaFactory(); PizzaStore nyStore = new PizzaStore(nyFactory); nyStore.order(“Veggie”);
ChicagoPizzaFactory chicagoFactory = new ChicagoPizzaFactory(); PizzaStore chicagoStore = new PizzaStore(chicagoFactory); chicagoStore.order(“Veggie”);
you are here 4 119
Then we create a PizzaStore and pass it a reference to the NY factory.
...and when we make pizzas, we get NY-styled pizzas.
Likewise for the Chicago pizza stores: we create a factory for Chicago pizzas and create a store that is composed with a Chicago factory. When we make pizzas, we get the Chicago flavored ones
Not what you want in a good franchise. You do NOT want to know what he puts on his pizzas.