Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Luca Vigan
Dipartimento di Informatica Universit di Verona
Factory
1 / 27
Factory
3 / 27
Factory
4 / 27
Factory
5 / 27
Factory
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.
Factory
8 / 27
Factory
9 / 27
Factory
10 / 27
Factory
11 / 27
Factory
12 / 27
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.
Factory
13 / 27
Put the createPizza() method back into PizzaStore but this time as an abstract method, and then create a PizzaStore subclass for each regional style.
Factory
14 / 27
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
Factory
16 / 27
Factory
17 / 27
Factory
18 / 27
Factory
19 / 27
Brain Teaser
Factory
20 / 27
Factory
21 / 27
Factory
22 / 27
Factory
23 / 27
Factory
24 / 27
Factory
25 / 27
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.
Factory
26 / 27
Factory
27 / 27