Sei sulla pagina 1di 27

The Simple Factory and Factory Pattern

Luca Vigan
Dipartimento di Informatica Universit di Verona

Laboratorio di Ingegneria del Software 13.05.2011

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

1 / 27

Identify the aspects that vary


Lets say you have a pizza shop, and you might end up writing some code like this:
Pizza o r d e r P i z z a ( ) { Pizza p i z z a = new Pizza ( ) ; p i z z a . prepare ( ) ; p i z z a . bake ( ) ; pizza . cut ( ) ; p i z z a . box ( ) ; return pizza ; }

For exibility, we really want this


Pizza p i z z a = new Pizza ( ) ;

to be an abstract class or interface, but we cant directly instantiate either of those.


Luca Vigan (Universit di Verona) Factory Lab. Ing. del SW, 13.05.2011 2 / 27

But you need more than one type of pizza...


So then youd add some code that determines the appropriate type of pizza and then goes about making the pizza

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

3 / 27

But the pressure is on to add more pizza types


You realize that all of your competitors have added a couple of trendy pizzas to their menus: Clam Pizza and Veggie Pizza. And you decide to take Greek Pizza off the menu:

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

4 / 27

Encapsulating object creation


Wed be better off moving the object creation out of the orderPizza() method. But how?

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

5 / 27

Building a simple pizza factory


Were going to dene the class SimplePizzaFactory that encapsulates the object creation for all pizzas.

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

6 / 27

Questions
Whats the advantage of this? It looks like we are just pushing the problem off to another object. One thing to remember is that the SimplePizzaFactory may have many clients. Weve only seen the orderPizza() method; however, there may be a PizzaShopMenu class that uses the factory to get pizzas for their current description and price. We might also have a HomeDelivery class that handles pizzas in a different way than our PizzaShop class but is also a client of the factory. So, by encapsulating the pizza creating in one class, we now have only one place to make modications when the implementation changes. Dont forget, we are also just about to remove the concrete instantiations from our client code!
Luca Vigan (Universit di Verona) Factory Lab. Ing. del SW, 13.05.2011 7 / 27

Questions (2)

Ive seen a similar design where a factory like this is dened as a static method. What is the difference? Dening a simple factory as a static method is a common technique and is often called a static factory. Why use a static method? Because you dont need to instantiate an object to make use of the create method. But remember it also has the disadvantage that you cant subclass and change the behavior of the create method.

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

8 / 27

Reworking the PizzaStore class


Back to the client code. We rely on the factory to create the pizzas for us. Here are the changes:

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

9 / 27

Simple Factory dened


Simple Factory isnt actually a real Design Pattern: its more of a programming idiom, but it is commonly used and some developers do mistake this idiom for the Factory Pattern". Lets take a look at the class diagram of our new Pizza Store:

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

10 / 27

Franchising the pizza store


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...), depending on where the franchise store is located and the tastes of the local pizza connoisseurs.

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

11 / 27

Franchising the pizza store


With the SimplePizzaFactory idiom, we create three different factories, then we can just compose the PizzaStore with the appropriate factory.

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

12 / 27

But youd like a little more quality control

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: theyd bake things a little differently, theyd forget to cut the pizza and theyd use third-party boxes. Rethinking the problem a bit, you see that what youd really like to do is create a framework that ties the store and the pizza creation together, yet still allows things to remain exible. There is a way to localize all the pizza making activities to the PizzaStore class, and yet give the franchises freedom to have their own regional style.

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

13 / 27

A framework for the pizza store


1

Put the createPizza() method back into PizzaStore but this time as an abstract method, and then create a PizzaStore subclass for each regional style.

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

14 / 27

Allowing the subclasses to decide


Remember: the Pizza Store already has a well-honed order system in the orderPizza() method and you want to ensure that its consistent across all franchises. What varies among the regional PizzaStores is the style of pizzas they make:
New York Pizza has thin crust Chicago Pizza has thick crust

and were going to push all these variations into the createPizza() method and make it responsible for creating the right kind of Pizza. The way we do this is by letting each subclass of PizzaStore dene what the createPizza() method looks like. So, we will have a number of concrete subclasses of PizzaStore, each with its own pizza variations, all tting within the PizzaStore framework and still making use of the well-tuned orderPizza() method.
Luca Vigan (Universit di Verona) Factory Lab. Ing. del SW, 13.05.2011 15 / 27

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

16 / 27

How do subclasses decide?

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

17 / 27

How do subclasses decide?

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

18 / 27

Lets make a PizzaStore


Now, you get all the PizzaStore functionality for free. All the regional stores need to do is subclass PizzaStore and supply a createPizza() method that implements their style of Pizza. Heres the New York regional style:

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

19 / 27

Brain Teaser

Write the Chicago and California PizzaStore implementations...

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

20 / 27

Declaring a factory method


With just a couple of transformations to the PizzaStore weve gone from having an object handle the instantiation of our concrete classes to a set of subclasses that are now taking on that responsibility.

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

21 / 27

Declaring a factory method


With just a couple of transformations to the PizzaStore weve gone from having an object handle the instantiation of our concrete classes to a set of subclasses that are now taking on that responsibility.

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

22 / 27

The Factory Method Pattern: the Creator classes

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

23 / 27

The Factory Method Pattern: the Product classes

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

24 / 27

Parallel class hierarchies

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

25 / 27

Factory Method Pattern dened

The Factory Method Pattern denes an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. As with every factory, the Factory Method Pattern gives us a way to encapsulate the instantiations of concrete types.

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

26 / 27

Factory Method Pattern dened: the class Diagram

Luca Vigan (Universit di Verona)

Factory

Lab. Ing. del SW, 13.05.2011

27 / 27

Potrebbero piacerti anche