Warning

This material has not yet been marked as "ready".

It should be treated as a "draft", i.e. a "work in progress". If this is an assignment, and this message still appears after the "assigned" date, please post a question on Piazza requesting clarification.
1
h12
CS56 W18
Name:
(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"

h12: Observer Design Pattern (HFDP 2)

ready? assigned due points
false Tue 10/31 09:30AM Tue 11/07 09:30AM

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 four lowest scores (if you have zeros, those are the four lowest scores.)


Reading Assignment: HFDP is “Head First Design Patterns”, the second required textbook for the course. Please Read HFDP Chapter 2 along with the online reading notes. Then, answer the questions below.

PLEASE DO NOT STAPLE or otherwise connect the pages if you turn in on two sheets of paper. Please just write your name on both and turn in as separate sheets. Stapling, taping, etc. jams the scanner and costs money for repairs!

  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. (20 pts) Chapter 2 mentions that Java Swing implements the Observer pattern. Briefly describe how the Observer pattern is used in Java Swing.

  3. (10 pts) When implementing the observer pattern, there are observers, and subjects. If you are writing your own code that implements the pattern, which of these is more likely to be true?

    Explain your answer.

    1. Each subject maintains a list of observers
    2. Each observer maintains a list of subjects
  4. The observer pattern is one way of implementing the principle of "loose coupling".
    1. (10 pts) What is the principle of “loose coupling”?

    2. (10 pts) What is the benefit of using “loose coupling” when writing software?

    3. (10 pts) How, specifically, does the observer pattern help to provide these benefits?

  5. The `java.util` package contains an `Observable` interface and an `Observer` class. HFDP Chapter 2 describes two implementations of a Weather display device. In each case, there is an object that is responsible for encapsulating the current conditions: temperature, humidity, and pressure. * The first of these two implementations implements its own version of the observer pattern from scratch * The second one uses `java.util.Observable` and `java.util.Observer`.
    1. (10 pts) One of these uses the pull model, and the other one uses the push model. Are these words “push” and “pull” written from the observer’s point of view, or the subject’s point of view?

    2. (5 pts) Describe one advantage of the pull model.

    3. (5 pts) Describe one advantage of the push model.

    4. (10 pts) In the java.util. version of the code, there is a setChanged() method. What is the purpose of that method in the code?

http://UCSB-CS56-F16.github.io/hwk/h08