Computer Science hwk
1. Strategy Pattern and sales chart display
Suppose that you have a SalesData class with a drawGraph() method. Draw a UML diagram and write a few lines of code to show how to use the Strategy Pattern to select at runtime between drawing a bar graph or a pie graph.
2. Template Method pattern and opening diverse file types
When you use your IDE, you can open and edit various types of files (e.g. text, code, and image files). The source code of your IDE has an openFile(String filename) method. Some of its code acts the same no matter what kind of file you open, but some of the code acts differently depending on the kind of file. Describe how this might be accomplished by using the Template Method pattern. Draw a UML diagram and write out some very rough code, using made-up names for the functions.
3. Command pattern and a word-processor's Undo and Redo
You probably have already used Ctrl+Z (for Undo) and Ctrl+Y (for Redo) in your word processor. What do you think might be a WordProcessorCommand in this context? What are the methods that WordProcessorCommand must implement? What kind of data structure might you need for holding the WordProcessorCommand objects? Draw a simple UML class diagram.
4. Composite pattern and building a virtual city
Suppose you want to develop a Structure class for describing doors, walls, rooms, floors of buildings, and buildings within a city. Draw a UML class diagram showing how to do this using the Composite pattern.
5. Singleton pattern and preserving a connection
Several parts of the source code of your system access an external stateless service. This service requires that you call the OpenConnection() method with your credentials as a parameter before calling the other methods. Everything was running fine. However, the service changed its policies and does not allow anymore to create multiple connections for the same credential. Your software started to fail when there are two methods that access the external service at the same time, because both open a connection to the service. How would you use the singleton pattern to avoid this
problem? Draw a UML diagram.
6. Bridge pattern and multiple implementations of a repository
In your system, users will be able to select how they will store the “Products” and the “Consumers.” They can choose one of the following options: “Database,” “XML File,” and “Cloud.” For example, a given user can choose to store their products in an XML file and their consumers in the cloud, while another user can store both products and consumers in the database. Use the bridge design pattern to separate the abstraction of object repository and the implementation of the different storages. Draw a UML diagram.
7. Observer pattern and stock updates
In your system, there is a class named Stock with an attribute named value. Your system has several classes that must react to the changes in the value, e.g., MobileStockDisplay, WebStockDisplay, AutomaticStockDealer. Study the observer design pattern (it was not in the lecture) and draw a UML diagram showing how to use it in this system.