Skip to content

Latest commit

 

History

History
61 lines (31 loc) · 5.5 KB

e-chapter8.md

File metadata and controls

61 lines (31 loc) · 5.5 KB

Chapter 8 Practice Exercises

Exercise 1

Consider the following implementation of class Deck. Apply the Observer design pattern to make the class observable. Assume we have two types of concrete observers: a DrawLogger with prints out a description of any card drawn, and a SizeStatus observer that prints out the size of the deck whenever it changes. Design and implement all required classes and interfaces, and write a small piece of client code to demonstrate the operation of the pattern. Use the push data-flow strategy. The concrete observers can be implemented as simple println statements.

Exercise 2

Change the design of your observable Deck from exercise 1 to use the pull data-flow strategy, using the interface segregation principle to strengthen the constraint that the observers should not change the state of the Deck object.

Exercise 3

Change the design of your observable Deck from exercise 1 so that all the observer-supporting code is in a superclass of deck. What are the advantages and disadvantages of this decision?

Exercise 4

Change the design of your observable Deck from exercise 1 so that all the observer-supporting code is in a subclass of deck. What are the advantages and disadvantages of this decision?

Exercise 5

Model the solution produced in exercise 4 with a class diagram, object diagram, and sequence diagram that illustrates a call initiated by calling draw on an ObservableDeck object.

Exercise 6

Apply the Observer design pattern to make it possible to observe class Program from exercise 6.14. Use the push data-flow strategy and a single observer interface. Create a dummy concrete observer class to test the application of the pattern.

Exercise 7

Change the design produced in Exercise 6 to use the pull data-flow strategy and a two different observer interfaces, one for addition events, and one for clearing and removing shows from the program. Use the interface segregation principle to ensure observers do not reference methods that can change the state of the subject.

Exercise 8

Extend the LuckyNumber JavaFX application to support an additional panel that shows the number in Roman numerals. For an extra challenge, parameterize the text panel so that it is possible to use a single class to create different representations of the number.

Exercise 9

Extend the UserInterface skeleton JavaFX program to create a GUI with a single Label and a single Button, where the label shows the current date and time, and gets updated every time the button is clicked. Try to create the solution by using only the API documentation, without looking at sample code. You will also need a Pane (consider using a subclass). For a quick solution to obtain the current time, you can use LocalDateTime.now().toString().

Exercise 10

Extend the UserInterface skeleton JavaFX program to create a GUI with two TextField instances and a single Button, where any text in the two text fields is swapped when the button is clicked. Organize the logical arrangement of the GUI components so that both text fields are in one parent, and the button is a child of the root.

Exercise 11

Extend Exercise 10 to embed the node that contains the textfields in a ScrollPane. Draw an object diagram that shows your component graph. What design patterns are being applied in this design? Add the event handler to your object diagram.

Exercise 12

Consider the sample class Node class hierarchy to support a file system. Apply the Visitor pattern to support the traversal of object graphs created from this class hierarchy. Implement the traversal code in the Node hierarchy, then write client code to try out your visitor support. Write a PrintVisitor to pretty-print all the nodes in a sample file tree. Try indenting the names of the nodes based on their depth in the tree. Draw a sequence diagram for the visit of a directory with one file and one symbolic link.

Exercise 13

Change the design of Exercise 12 to include an AbstractVisitor class. Move the traversal code to this class, and change your PrintVisitor so it extends AbstractVisitor. Draw a sequence diagram for the visit of a directory with one file and one symbolic link, and make sure to see the difference with the diagram created as part of exercise 12.


Creative Commons License

Unless otherwise noted, the content of this repository is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

Copyright Martin P. Robillard 2019-2021