1
h05
CS56 S19
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu section
4pm,5pm,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"

h05: HFDP 1, 2: Intro to Design Patterns, Strategy, Observer

ready? assigned due points
true Mon 05/13 05:00PM Mon 05/20 05:00PM

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

MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the two lowest scores (if you have zeros, those are the two lowest scores.)


Reading Assignment:

  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,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) On pg 11, the author’s approach to fixing the Duck simulator is to “use an interface to represent each (duck) behavior … and each implementation of a behavior will implement one of those interafaces.” What is the main motivation / advantage in “Programming to an interface” in this context.

  3. (Thanks to Giovanni R.). Refer to the code under the heading “Setting Behavior Dynamically” on pg 20.

    1. (10 pts) What parts of the code on this page allow the user to set duck behavior dynamically at run-time?

    2. (10 pts) Why would setting behavior dynamically not be possible if we had a concrete implementation that lives inside of the duck class?

  4. The author describes one of the benefits of design patterns as being that of a “shared vocabulary”.

    1. (10 pts) What does the author mean by “shared vocabulary”?

    2. (10 pts) How is a “shared vocabulary” of design patterns useful to software developers?

  5. In HFDP Chapter 2, the implementation of the WeatherData class on pg. 43 will update the weather displays when measurements have changed.

    1. (10 pts) If additional displays are created and need to be updated by the WeatherData class, briefly explain why this implementation is not easily expandable.

    2. (10 pts) How does having Observer objects implement an Observer Interface solve this problem? Explain this in your own words (Hint, the answer is summarized on pg 53).

  6. In the Observer implementation on pg. 57-59, the author describes this approach as pushing state information.

    1. (5pts) Briefly describe what the author means by “pushing” state?

    2. (5pts) Briefly describe what the author means by “pulling” state?

  7. (10 pts) The author describes that there is already an Observable class (java.util.Observable) that can be used in implementing the Observer pattern (and also provides a modified WeatherStation implementation using it). What are the two disadvantages of using java.util.Observable (specifically since it is a class)?