1
h12
CS56 M16
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu section
9am or 10:30am
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: The Decorator Pattern (HFDP 3), Serialization (HFJ 14)

ready? assigned due points
true Fri 07/15 07:00PM Wed 07/20 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 three lowest scores (if you have zeros, those are the three lowest scores.)


This homework concerns HFDP Chapter 3, and HFJ Chapter 14. Please read those chapters, and then answer these questions.

  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 (either 9am or 10:30am) in the space indicated. 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. In Chapter 3, the Open/Closed principle is presented in the context of the Decorator pattern.
    1. (10 pts) Fill in the two blanks to result in the statement of this principle.

      Open for ______________________________________ closed for __________________________________.

    2. (10 pts) Ok, that was the easy part; you just had to find the words in the chapter. Now, the harder part: explain what this means in your own words. "In your own words" means, don't just write down words for the book. Read the book, then put the book down, and write what you've learned as if you were explaining it to someone.
  3. (10 pts) The standard Java library classes that implement input and output streams use the Decorator pattern. Describe how the Decorator pattern is used here, and what benefits it provides.
  4. The decorator pattern features both composition and inheritance.
    1. (10 pts) How is composition used in the Decorator pattern?
    2. (10 pts) How is inheritance used in the Decorator pattern? (Note: there is a particular purpose for which inheritance is used in the Decorator pattern. The book describes this very specifically.)
  5. (5 pts) Suppose a certain object implements the Serializable interface. There is some part of the object that should NOT be saved when serializing the object. How do you indicate this in your code?

  6. (15 pts) For this question, consider the reading in HFJ about serialization. Your friend B. C. Dull says: "I don't get why object serialization is such a big deal. You have a pointer to each object, and you know how many bytes it takes up in memory, and you know the objects type. Just write those bytes to a file, along with a few extra bytes indicating the type—problem solved. Then you read those bits back in and restore the objects. What's the big deal". You however, see more deeply into the situation, and say: "Well, B.C., it isn't quite that simple. You are forgetting a few subtle issues—things you should realize from your previous study of how data structures work—things you should have learned in CS16 and CS24. Even though those were C/C++ courses, the same problems are going to arise in Java." B.C. says: "I don't see what you are getting at. Can you explain it to me?" What do you say to B.C. to help him/her realize why Object Serialization is more subtle than just writing out all the bits exactly as they are, and reading them back in exactly as they are? HINT: Think about things like linked lists, pointers, and references, and how they are implemented in both C++ and Java.
  7. (20 pts) Write a brief main method that writes the string Hello Disk! to a file called myFirstFile.txt. You only need the main method, not the enclosing class. Be sure to account for any exceptions that must be declared or caught.

http://UCSB-CS56-M16.github.io/hwk/h12