Assignment #5: Design Patterns


Assigned:   Monday, Apr. 3
Due:   Wednesday, Apr. 12

Contents:


Overview

Topic(s): Design Patterns, Class Relationships, UML Diagrams, and GUIs
Related Reading: Chapters 5 and 6


Practice Problems

  1. Exercise #5.1 at the end of Chapter 5 (p. 212)

    To see the solution, go to Solutions, and select Chapter 5, Exercise 1.

  2. Exercise #5.3 at the end of Chapter 5 (p. 212)

    To see the solution, go to Solutions, and select Chapter 5, Exercise 3.

  3. Exercise #5.7 at the end of Chapter 5 (p. 213)

    To see the solution, go to Solutions, and select Chapter 5, Exercise 7.

  4. Exercise #5.11 at the end of Chapter 5 (p. 213)

    To see the solution, go to Solutions, and select Chapter 5, Exercise 11.

  5. Exercise #5.15 at the end of Chapter 5 (p. 213)

    To see the solution, go to Solutions, and select Chapter 5, Exercise 15.

  6. Exercise #6.17 at the end of Chapter 6 (p. 259)

    To see the solution, go to Solutions, and select Chapter 6, Exercise 17.


Problems to be Submitted (20 points)

When you turn in your assignment, you must include a signed cover sheet (PDF version) with your assignment (you're assignment will not be graded without a completed cover sheet).

You are allowed to submit your assignment via email, but if you choose to do so, you must bring a hardcopy of your assignment along with a completed cover sheet to the instructor at the next class. (Note: Do not email the instructor any .zip file attachments, as SLU's email may not deliver these emails, i.e. the instructor may not receive your email.)

Note: For this assignment you are allowed, even encouraged, to work in groups of two (i.e. two students work together and turn in one set of answers).

  1. (2 points)

    Exercise #6.18 at the end of Chapter 6 (p. 259)

  2. (2 points)

    As noted on p. 252 of the textbook, the Rectangle2D and Rectangle2D.Double classes use the TEMPLATE pattern. Explain in your own words how these two classes use the TEMPLATE pattern.

  3. (3 points)

    Most applications for viewing and editing images allow the user to view a variety of different image types, both compressed and uncompressed, such as PNG, JPEG, GIF, TIFF, BMP, etc. These applications likewise typically allow the user to save the files to any of these formats, so the user can either save after editing, or simply convert the original to a different format (i.e. load PNG and save as JPEG, etc.).

    Explain how the STRATEGY pattern enables this flexibility with the different image formats.

  4. (3 points)

    Imagine a situation in which you're writing some accounting software for a company, and you're tasked with the job of writing some code that manages all the orders (with one or more products per order) for each client. A key goal is that the software should be able to generate a full list of all products sold each month (both to each client, and to all clients) and the gross sales and net profits each month. Explain why the COMPOSITE pattern would be helpful in this situation.

  5. (3 points)

    In the code for the Checkers program from the last homework (Hwk #4), indicate whether any of the following design patterns were used in that code.

    If a design pattern was used somewhere in the code, please indicate where/how it was used.

    Note/Hint: Please include any design patterns employed by the various Java classes used in the code - e.g. JPanel, etc.

  6. (7 points)

    Section 6.5 modifies the SceneEditor example (i.e. the SelectableShape and DrawingCanvasComponent example) one step further, introducing the CompoundShape class. Using this class, the various Shape classes (e.g. CarShape, HouseShape, etc.) no longer need to implement any of the methods in SelectableShape (or SceneShape), and only need to specify the structure of the shape in the constructor.

    While the class name, CompoundShape, somewhat implies a design corresponding to the COMPOSITE pattern, at present it does not actually use the COMPOSITE pattern (over SelectableShapes). So, let's modify the code to change this...

    To facilitate this process, following is a new version of the author's code, which adds additional constructors (without the x,y offset) and renames CompoundShape to CustomShape. The renamed file, CustomShape, will serve as our "Leaf" class, and we'll make a separate "Composite" class called CompositeShape. The addition of the extra constructors is simply for a cleaner final design.

    You can download these files individually, or altogether via the zip file SceneEditor_v3_new.zip:

    In order to modify this code to use the COMPOSITE pattern, you'll need to do the following:

    In order to demonstrate that your changes for the COMPOSITE pattern work, also do the following: