Design pizza creation Encapsulating object creation Where is the - - PowerPoint PPT Presentation

design pizza creation encapsulating object creation where
SMART_READER_LITE
LIVE PREVIEW

Design pizza creation Encapsulating object creation Where is the - - PowerPoint PPT Presentation

F ACTORY METHOD PATTERN Hongbin Lu Design pizza creation Encapsulating object creation Where is the creational code? We should place the creational code in an object. That object is only going to worry about how to create pizzas.


slide-1
SLIDE 1

FACTORY METHOD PATTERN

Hongbin Lu

slide-2
SLIDE 2

Design pizza creation

slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5

Encapsulating object creation

slide-6
SLIDE 6

Where is the creational code?

  • We should place the creational code in

an object.

  • That object is only going to worry about

how to create pizzas.

  • We call that “simple pizza factory”.
slide-7
SLIDE 7

Our simple pizza factory

slide-8
SLIDE 8
slide-9
SLIDE 9
slide-10
SLIDE 10

Franchising the pizza store

  • NY style pizzas: thin crust, tasty sauce and

just a little cheese.

  • Chicago style pizzas: thick crust, rich

sauce, and tons of cheese.

  • We need two factories to produce

different styles of pizzas.

slide-11
SLIDE 11

Franchising the pizza store

slide-12
SLIDE 12

Franchising the pizza store

New York: NYPizzaFactory nyFac = new NYPizzaFactory(); PizzaStore nyStore = new PizzaStore(nyFac); nyStore.order(“Veggie”); Chicago: ChicagoPizzaFactory chiFac = new ChicagoPizzaFactory(); PizzaStore chiStore = new PizzaStore(chiFac); chiStore.order(“Veggie”);

slide-13
SLIDE 13

A framework for pizza store

slide-14
SLIDE 14

Let’s make a PizzaStore

slide-15
SLIDE 15

The Creator classes

slide-16
SLIDE 16

Look closer to the factory method

Public abstract class PizzaStore { … Protected abstract Pizza createPizza(String type); …. }

slide-17
SLIDE 17

The Pizza class

Public abstract class Pizza { String name; String dough; String sauce; ArrayList toppins = new ArrayList(); void prepare() {…} void bake() {…} void cut() {…} void box() {…} }

slide-18
SLIDE 18

Public class NYStyleCheesePizza extends Pizza { public NYStyleCheesePizza() { name = “NY Style Sauce and Cheese Pizza”; dough = “Thin Crust Dough”; sauce = “Marinara Sauce”; toppings.add(“Grated Reggiano Cheese”); } }

The NY Style Cheese Pizza

slide-19
SLIDE 19

Public class ChicagoStyleCheesePizza extends Pizza { public ChicagoStyleChesePizza() { name = “Chicago Style Deep Dish Cheese Pizza”; dough = “Extra Thick Crust Dough”; sauce = “Plum Tomato Sauce”; toppings.add(“Shredded Mozzarella Cheese”); } void cut() { // Cutting the pizza into square slices } }

The Chicago Style Pizza

slide-20
SLIDE 20

The Product classes

slide-21
SLIDE 21

Public class PizzaClient { public static void main(String[] args) { PizzaStore nyStore = new NYPizzaStore(); PizzaStore chicagoStore = new ChicagoPizzaStore(); // order NY Style cheese Pizza pizza = nyStore.orderPizza(“cheese”); // order Chicago Style cheese Pizza pizza2 = chicagoStore.orderPizza(“cheese”); } }

The Client

slide-22
SLIDE 22
slide-23
SLIDE 23

Definition

The Factory Method Pattern defines an interface for creating an objects, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

slide-24
SLIDE 24
slide-25
SLIDE 25
slide-26
SLIDE 26
slide-27
SLIDE 27

QUESTIONS?

slide-28
SLIDE 28

REFERENCES

Head First Design Patterns, Eric F, Elisabeth F, 2004