22
CSC207 Week 8 Larry Zhang 1

CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

CSC207 Week 8Larry Zhang

1

Page 2: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

Design Patterns Continued ...

2

Page 3: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

The Factory Pattern

3

Page 4: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

When to use the Factory pattern

● This is one of the most used design patterns in practice.

● Creates objects without exposing the instantiation logic to the client.

○ Food food = FoodFactory.createProduct(“Burger”);

○ food is an Burger object

● Refers to the newly created object through a common interface.

○ food.getName();

○ food.getCalories();

○ food.eat();

4

Page 5: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

5

Page 6: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

Other factory examples ...

ShapeFactory factory = new ShapeFactory();

Shape s1 = factory.createProduct(“circle”);

Shape s2 = factory.createProduct(“square”);

The product of a factory could also be a strategy, a command, etc.

6

Page 7: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

UML of the Factory pattern

7

Page 8: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

How to implement the Factory pattern

● Create a base class or interface for the product

○ e.g., Food

● Implement concrete product classes by extending the base class

○ e.g., Burger, Pizza, Salad, etc.

● Create the Factory class with a createProduct() method

○ ProductBase createProduct(String productID)

○ return objects of different types according to productID.

8

Page 9: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

DEMO #1: Food Factory

Food.java

Burger.java

Fries.java

Pizza.java

Salad.java

Soda.java

FoodFactory.java

ClientMain.java

9

Page 10: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

Advanced Issues in Factory pattern

● In our example, the factory’s products are hard-coded. A more flexible implementation would be to allow dynamically registering products.

○ maintain a HashMap: productID -> concrete product class

○ How to store a “class type” in the map?

○ Read: http://www.oodesign.com/factory-pattern.html

● Other related patterns:

○ abstract factory: http://www.oodesign.com/abstract-factory-pattern.html

○ factory method: http://www.oodesign.com/factory-method-pattern.html

10

Page 11: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

The Builder Pattern

11

Page 12: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

When to use the Builder pattern

● It’s also about creating objects. But it’s different from the Factory pattern.

● The Factory pattern is good for simple creation process

○ given a productID, returns the product, e.g, “Burger” -> Burger

● The Builder pattern is for complex creation process

○ we want to customize many attributes of the object

○ e.g., How you order a Pizza: add extra cheese, add sauce, add tomato …

● The Builder pattern separates object construction from its representation.

○ The client doesn’t need to see the constructor of the product class (e.g., Pizza)

12

Page 13: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

13

Page 14: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

How to implement the Builder pattern

14

● Define the Product class with different attributes (to be customized) and their setter methods.

● Define a Builder class that keeps the options for setting the Product's attributes, and has methods (buildParts()) for building different parts of the product.

● Builder has a getProduct() method that create a Product object, configures its attributes, and returns the Product object.

Page 15: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

DEMO #2: Pizza Builder

Pizza.java

PizzaBuilder.java

ClientMain.java

15

Page 16: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

Concrete Builder

You can extend from the Builder and make a concrete builder with a specific configuration.

16

Page 17: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

DEMO #3: Concrete BuilderHawaiianPizzaBuilder.java

DeluxePizzaBuilder.java

ClientMain.java

17

Page 18: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

Director

You can also use a Director to create a bunch of products.

18

Page 19: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

Relation between Client, Director, Builder

19

Page 20: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

DEMO #4: Director

PizzaDirector.java

ClientMain.java

20

Page 21: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

Chain Builder

By modifying the Builder a bit, you can construct a Product like the following.

21

Page 22: CSC207 Week 8 - Department of Computer Science, University ...ylzhang/csc207/files/lec08.pdf · given a productID, returns the product, e.g, “Burger” -> Burger The Builder pattern

DEMO #5: Chain BuilderPizzaChainBuilder.java

ClientMain.java

22