the simple factory pattern and factory method patternjin/3132/lectures/dp-07.pdf · the simple...
TRANSCRIPT
![Page 1: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/1.jpg)
The Simple Factory Pattern and
Factory Method Pattern
CSCI 3132 Summer 2011 1
![Page 2: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/2.jpg)
New is an implementa-on
• Calling “new” is certainly coding to an implementa-on
• In fact, it’s always related to a concrete class • (Open for Modifica-on)
• That’s fine when things are simple, but . . .
2
![Page 3: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/3.jpg)
Look Out For Change
• When you have several related classes, that’s probably a good sign that they might change in the future
Duck duck;!if (picnic) {!! duck = new MallardDuck();!} else if (hunting) {!! duck = new DecoyDuck();!} else if (inBathTub) {!! duck = new RubberDucky();!}!
3
![Page 4: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/4.jpg)
Instan-a-ng Concrete Classes
• Using new instan-ates a concrete class. • This is programming to implementa-on instead of interface.
• Concrete classes are oKen instan-ated in more than one place.
• Thus, when changes or extensions are made all the instan-a-ons will have to be changed.
• Such extensions can result in updates being more difficult and error-‐prone.
4
![Page 5: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/5.jpg)
Example
Suppose that you are required to develop a
system that accepts orders for pizzas. There
are three types of pizzas, namely, cheese,
Greek, and pepperoni. The pizzas differ
according to the dough used, the sauce used
and the toppings.
Draw a class diagram for the system.
5
![Page 6: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/6.jpg)
Design Principle • What should you try to do with code that changes?
Pizza orderPizza() {!! Pizza pizza = new Pizza(); !
! pizza.prepare();!! pizza.bake();!! pizza.cut();!! pizza.box();!
! return pizza;!}!
6
![Page 7: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/7.jpg)
Pizza orderPizza(String type) {!! Pizza pizza;!
! if (type.equals(“cheese”)) {!! !pizza = new CheesePizza();!! } else if (type.equals(“greek”)) {!! !pizza = new GreekPizza();!! } else if (type.equals(“pepperoni”)) {!! !pizza = new PepperoniPizza();!! }!
! pizza.prepare();!! pizza.bake();!! pizza.cut();!! pizza.box();!
! return pizza;!}! 7
![Page 8: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/8.jpg)
Example:Class Diagram
Problems with the design 8
![Page 9: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/9.jpg)
Pizza orderPizza(String type) {!! Pizza pizza;!
! if (type.equals(“cheese”)) {!! !pizza = new CheesePizza();!! } else if (type.equals(“greek”)) {!! !pizza = new GreekPizza();!! } else if (type.equals(“pepperoni”)) {!! !pizza = new PepperoniPizza();!! } else if (type.equals(“sausage”)) {!! !pizza = new SausagePizza();!! } else if (type.equals(“veggie”)) {!! !pizza = new VeggiePizza();!! }!
! pizza.prepare();!! pizza.bake();!! pizza.cut();!! pizza.box();!! return pizza;!}!
9
![Page 10: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/10.jpg)
Pizza orderPizza(String type) {!! Pizza pizza;!
! if (type.equals(“cheese”)) {!! !pizza = new CheesePizza();!! } else if (type.equals(“greek”)) {!! !pizza = new GreekPizza();!! } else if (type.equals(“pepperoni”)) {!! !pizza = new PepperoniPizza();!! } else if (type.equals(“sausage”)) {!! !pizza = new SausagePizza();!! } else if (type.equals(“veggie”)) {!! !pizza = new VeggiePizza();!! }!
! pizza.prepare();!! pizza.bake();!! pizza.cut();!! pizza.box();!! return pizza;!}!
Encapsulate!
10
![Page 11: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/11.jpg)
Revised System
• Suppose that the Greek pizza is not popular and must be removed and two new pizzas, Clam and Veggie, must be added to the menu.
• Programming to implementa-on like makes such changes difficult.
• Crea-ng a SimpleFactory to encapsulate the code that changes will make the design more flexible.
• Remove the code that creates a pizza – forms a factory.
11
![Page 12: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/12.jpg)
public class SimplePizzaFactory {!! public Pizza createPizza(String type) {!! ! Pizza pizza;!
! ! if (type.equals(“cheese”)) {!! ! ! pizza = new CheesePizza();!! ! } else if (type.equals(“pepperoni”)) {!! ! ! pizza = new PepperoniPizza();!! ! } else if (type.equals(“sausage”)) {!! ! ! pizza = new SausagePizza();!! ! } else if (type.equals(“veggie”)) {!! ! ! pizza = new VeggiePizza();!! ! }!
! ! return pizza;!! }!}!
Now orderPizza() is tidy
12
![Page 13: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/13.jpg)
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.cute();!! ! pizza.box();!
! ! return pizza;!! }!}!
No new operator
No new() operator
13
![Page 14: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/14.jpg)
Example: Revised Class Diagram
14
![Page 15: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/15.jpg)
Simple Factory
• Pull the code that builds the instances out and put it into a separate class – Iden-fy the aspects of your applica-on that vary and separate them from what stays the same
15
![Page 16: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/16.jpg)
Simple Factory Pa]ern
Client orderProduct()
SimpleFactory createProduct()
<<abstract>> Product
productMethod()
ConcreteProductB
ConcreteProductA
ConcreteProductC
16
![Page 17: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/17.jpg)
Why would we do this?
• Mul-ple clients needing same types of object • Ensure consistent object ini-aliza-on
17
![Page 18: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/18.jpg)
Example: System Revision Again
Franchises in different parts of the country are now adding their own special touches to the pizza. For example, customers at the franchise in New York like a thin base, with tasty sauce and li]le cheese. However, customers in Chicago prefer a thick base, rich sauce and a lot of cheese. Some franchises also cut the pizza slices differently (e.g. square)
You need to extend the system to cater for this.
18
![Page 19: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/19.jpg)
The Factory Method
Defini-on: • The factory Method Pa]ern defines an interface for crea-ng an object, but lets the subclasses decide which class to instan-ate.
• Factory Method lets a class defer instan-a-on to subclasses.
19
![Page 20: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/20.jpg)
Creator Class
20
![Page 21: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/21.jpg)
Product Class
21
![Page 22: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/22.jpg)
public abstract class PizzaStore {!! !! public Pizza orderPizza(String type) {!! ! Pizza pizza;!! ! pizza = createPizza(type);!
! ! pizza.prepare();!! ! pizza.bake();!! ! pizza.cute();!! ! pizza.box();!
! ! return pizza;!! }! abstract Pizza createPizza(String type);!}!
No new operator
22
![Page 23: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/23.jpg)
public class NYPizzaStore extends PizzaStore{! Pizza createPizza(String item) {!! !! if (type.equals(“cheese”)) {!! !! ! return new NYStyleCheesePizza();!! !! } else if (type.equals(“pepperoni”)) {!! !! ! return new NYStylePepperoniPizza();!! !! } else if (type.equals(“veggie”)) {!! !! ! return new NYStyleVeggiePizza();!! !! } else return null;! }!}!
23
![Page 24: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/24.jpg)
Structure
24
![Page 25: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/25.jpg)
Factory Method Pa]ern
<<abstract>> Creator
factoryMethod() operation()
<<abstract>> Product
ConcreteCreator factoryMethod()
ConcreteProduct
abstract!
IS-A
HAS-A
IS-A
No more SimpleFactory class Object creation is back in our class, but … delegated to concrete classes
25
![Page 26: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/26.jpg)
Applicability
• Use Factory Method when: – A class can’t an-cipate the class of objects it must create
– A class wants its subclasses to specify the objects it creates
– Classes delegate to one of several helper subclasses, and you want to localize knowledge about which is the delegate
26
![Page 27: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/27.jpg)
Par-cipants
• Product – Defines interface of objects to be created
• ConcreteProduct – Implements Product interface
• Creator – Declares factory method
• Returns object of type Product • May define default implementa-on
– May call factory method to create Products
27
![Page 28: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/28.jpg)
Consequences
• Eliminates need to bind crea-on code to specific subclasses
• May need to subclass Creator for each ConcreteProduct
• Provides hooks for subclasses • Connects parallel class hierarchies
28
![Page 29: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/29.jpg)
An Example
• Applica-on framework – App subclasses “Applica-on,” “Document”
– Framework knows when to create Document, but Doesn’t know what type of Document to create
– Factory Method moves knowledge about specific Document subclass out of framework
29
![Page 30: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/30.jpg)
Guidelines
• No variable should hold a reference to a concrete class
• No class should derive from a concrete class
• No method should override an implemented method of an of its base classes.
30
![Page 31: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/31.jpg)
Summary
• The creator gives you an interface for wri-ng objects.
• The creator specifies the “factory method”, e.g. the createPizza method.
• Other methods generally included in the creator are methods that operate on the product produced by the creator, e.g. orderPizza.
• Only subclasses implement the factory method.
31
![Page 32: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/32.jpg)
Summary
• Simple Factory – Use when you have only one family of objects
• Factory Method Pa]ern – Use when you have mul-ple families of objects
• Abstract Factory (next -me) – Use when you have mul-ple families of object components
32
![Page 33: The Simple Factory Pattern and Factory Method Patternjin/3132/lectures/dp-07.pdf · The Simple Factory Pattern and Factory Method Pattern CSCI 3132 Summer 2011 1](https://reader030.vdocuments.net/reader030/viewer/2022041016/5ec782aa0d7d4674434d8707/html5/thumbnails/33.jpg)
Thoughts…
• When you have code that instan-ates concrete classes that may change, encapsulate the code that changes.
• The factory pa]ern allows you to encapsulate the behaviour of the instan-a-on.
• Duplica-on of code is prevented. • Client code is decoupled from actual implementa-ons.
• Programming to interface not implementa-on.
33