CS56 F16
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu section
4pm, 5pm or 6pm
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

h15: The Factory Pattern

ready? assigned due points
true Mon 10/31 12:30PM Thu 11/03 04:00PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

in place of that, we drop the three lowest scores (if you have zeros, those are the three lowest scores.)

This homework pertains to Chapter 4 in HFDP. Chapter 4 is long, so we are going to divide it into two homework assignments. The first pertains only to pages 109 through 137.

Please read those pages chapter, and any reading notes that may be online. The reading notes for HFDP are a work in progress, but slowly, a little bit at a time, I am adding some things there. So check back periodically as you read, work on the homework, and later as you prepare for the exams.

  1. (10 pts) Please fill in the information at the top of this homework sheet, including your name and umail address. Put the time your discussion section starts (4pm, 5pm or 6pm) in the space indicated (the one you are registered for—even if you usually attend a different one.) If the other two items apply, please fill them in as well. Please do this every single time you submit homework for this class.
  2. (10 pts) The chapter introduces the idea of a Simple Factory, that is, a class that creates objects. The `SimplePizzaFactory` class is an example. This class has only one method, `createPizza`. As an example of using this method, instead of: `Pizza p = new CheesePizza();`, we might write `Pizza p = createPizza('cheese');` According to the text, what is the benefit of using a Simple Factory to create objects, instead of just using `new` with a constructor? Note: Don't say "it allows us to create objects". That's "what" a Simple Factory does. I want to know the answer to the question "why do it that way?"
  3. The discussion of Simple Factory on p. 117 (or possibly p. 119, depending on your edition) discusses “concrete products” and their relationship to the SimplePizzaFactory. Although the book doesn’t use this word, there is also a notion of an “abstract product” lurking on this page.

    1. (10 pts) Give one example of a concrete product class mentioned on p. 117/119.

    2. (10 pts) What is the name of the class or interface referred to on p.117/119 that represents the abstract product produced by the SimplePizzaFactory?

  4. We are continuing our discussion of the Simple Factory from p. 117/119 from the previous question.

    1. (10 pts) What is the relationship between the SimplePizzaFactory class and the various “concrete product classes”?

    2. (10 pts) What is the relationship between the “abstract product class” and the various “concrete product classes”?

  5. (20 pts) The text differentiates between a Simple Factory, which it says “isn’t actually a design pattern” but rather a “programming idiom”. (For now, we’ll put off trying to pin down the difference between an “actual design patern” and something that is just “a programming idiom”)

    But, there is a design pattern in this section called the Factory Method Pattern. What is the difference between how the PizzaStore class is written when we use the Simple Factory, vs. the way it is written when we use the Factory Method Pattern?

    Focus on the place in the code where Pizza objects get created, and what the relationship of that code is to the PizzaStore class.

  6. (20 pts) Consider this diagram from p. 134. (In some editions, it may be on p. 136; it’s the page with the heading “Factory Method Pattern Defined”.)

    Factory Method Pattern

    For each of the roles in the diagram, indicate what class(es) or interface(s) is/are playing that role in the Pizza Store example of the Factory Method Pattern described on the pages that lead up to this diagram. Just write the name of the class (or classes), interface (or interfaces) beside the box.