Topic(s): Design Patterns, Class Relationships, UML Diagrams, and GUIs
Related Reading: Chapters 5 and 6
Exercise #5.1 at the end of Chapter 5 (p. 212)
To see the solution, go to Solutions, and select Chapter 5, Exercise 1.
Exercise #5.3 at the end of Chapter 5 (p. 212)
To see the solution, go to Solutions, and select Chapter 5, Exercise 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.
Exercise #5.11 at the end of Chapter 5 (p. 213)
To see the solution, go to Solutions, and select Chapter 5, Exercise 11.
Exercise #5.15 at the end of Chapter 5 (p. 213)
To see the solution, go to Solutions, and select Chapter 5, Exercise 15.
Exercise #6.17 at the end of Chapter 6 (p. 259)
To see the solution, go to Solutions, and select Chapter 6, Exercise 17.
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).
Exercise #6.18 at the end of Chapter 6 (p. 259)
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.
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.
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.
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.
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:
Notice that CustomShape explicitly indicates that it implements SceneShape? While it is not necessary to explicitly indicate this -- as CustomShape already implements SceneShape implicitly through SelectableShape -- doing so explicitly calls attention to the fact that we're going to use SceneShape as our "Primitive". Likewise, I recommend adding the same to CompositeShape.
In addition to passing in a SceneShape object, you'll also need to pass in an offset (x,y) which corresponds to its offset from the upper-left corner of the composite shape. This offset is needed to position smaller shapes appropriately within larger shapes.
Note: While the CompositeShape class will be somewhat similar to CustomShape, in that it needs to implement the draw(), translate(), and contains() methods, as well as the addShape() method (which is similar to CustomShape's add() method), it will NOT use the GeneralPath class to aggregate the shapes. GeneralPath acts like an ArrayList for Java's Shape class, and what we need in CustomShape is an ArrayList of SceneShape objects, so you'll need to use an ArrayList instead of GeneralPath. And because GeneralPath class internally implements the process of looping over all Shapes within in a path, your code in CompositeShape will need to explicitly implement that looping over all SceneShape objects within the translate(), contains(), and draw() methods.
In order to demonstrate that your changes for the COMPOSITE pattern work, also do the following: