Problems and Projects

Utility Classes versus Singletons

All of the methods and fields of a utility class are static. There is no point in creating instances of a utility class. For example, Java's Math class is a utility class containing standard mathematical functions:

class Math {
   public static double sin(double x) { ... }
   public static double cos(double x) { ... }
   public static double log(double x) { ... }
   // etc.
}

Only one instance of a singleton class can be created. Often a developer can choose between a utility class and a singleton class.

For example, a white page server contains a hash table that associates server names to server locations. The white page server provides a method that allows servers to register their names and locations, and provides a method that allows clients to locate servers using only their names. Of course all servers and clients will somehow need to know where the white page server is located. It might be confusing if an application has more than one white page server.

Here are the possible designs. First using Singleton:

sing

Second, using a utility class:

sing

UML note: An underlined method or field is static.

(a) Sketch an implementation of WhitePageServer, Server, and Client two times. In both cases your sketch should show how the client and server use the white page server. First implement WhitePageServer as a utility class, and then as a singleton class.

(b) Can you think of any advantages or disadvantages of singleton over utility or vice-versa?

Value vs. Reference Objects

We can have many non-mutable objects representing the same real world object because there's no danger that they will get out of synch with each other. For example, it's not a problem having multiple non-mutable Student objects representing the same student.

Having several mutable Student objects representing the same student is a problem, however, because they can be mutated in different ways and therefore become out of synch with each other.

Use interning to implement a mutable Student class that doesn't allow two student objects to be created that represent the same student.

Seeing Anti-Patterns

Criticize the following designs

(a)

bad1

(b)

bad2

(c)

bad3

(d)

bad4

(e)

bad5

(f)

bad6

Mock Objects

Sometimes, in early development, a "mock" object is needed so that testing can proceed while the real object is under development. A mock object implements the same interface as the object that will replace it.

A gateway has the following interface:

interface Gateway {
   Model getModel(String fname) throws AppError;
   void saveModelAs(Model model, String fname) throws AppError;
   void saveModel(Model m) throws AppError;
}

Where:

class Model implements Serializable {
   String fname = null; // name of file where this model is stored
   boolean usavedChanges = false; // true if modified since last save
}

Eventually, this interface will be implemented by an object that interfaces with a database system. For now, create a mock object that uses a hash table to store models and their names.

Implementing Singleton

There should be only on instance of a GUI. Show how you would use the Singleton pattern in the GUI class.

On line chess

Show how you would design the following system using UML class diagrams and design patterns.

A chess playing program allows multiple users to play chess against a centralized server. Users can save their games and resume them later.

A Workflow Management System (WMS)

Your team is developing an application that will create, browse, and update student records that will be stored in some sort of database. A student record contains a last name, first name, unique id number, major, and a GPA. Using the DAO and Transfer Object patterns develop an interface that the application can use to access the database. Your DAO should allow searches that return multiple records. (Hint: a null field matches anything.)

A Student Tracking System (STS)

After a student has been admitted to Northern California University, a student record is created in a database. This record holds the student's name, identification number, major, and grade point average. It also holds a list of all semesters the student attended. For each semester, there is a list of courses the student took. Each course has a department, number, title, number of units, and of course the grade the student received. Faculty and administrators may view and update student records. A student may view his own record.

Draw a domain model using a class diagram.

Draw a use case diagram.

How would you design STS? Consider using the following patterns: Three-Tier architecture, Model-View-Controller, Proxy, Gateway, and Transfer Objects.

Implement a student record in Java.

A Patient Tracking System

When a patient checks into the hospital, the hospital administrator creates a patient record in the Patient Tracking System (PTS). The record initially holds the patient's name, the name of his insurance company, his policy identification number, and the name of his doctor. After the patient has been admitted, his doctor places orders for tests on the patient's record. These tests are performed by technicians, who record the results in the patient record. Next the doctor orders treatments by placing them in the patient record. Nurses perform the treatment. When the patient checks out of the hospital, the administrator adds up the costs for the tests and treatments and generates an invoice.

Draw a UML class diagram showing how you would model this domain.

Write an interface for a DAO that transfers patient records between a database and the PTS using transfer objects.

A Claim Processing System

A claims processing system (CPS) provides a user interface that allows a claims adjuster to view and update policies, and to create, update, and view claims. When a claims adjuster receives an accident report, he first determines if the accident will be covered by the claimant's policy. If so, he creates a claim that describes the date of the claim and the policy number of the claimant. After the adjuster receives three repair estimates and a police report, a check is issued to the claimant, the claim is closed. The amount of the check is determined by the business rule:

lowest estimate - deductable

If, according to the police report, the claimant was at fault, then the policy premium is increased according to the business rule:

premium += riskFactor * premium

where

riskFactor =
   (#claims where the claimant was at fault)/# of years driving

Represent the description just given with a UML activity diagram.

Draw a UML diagram showing how you would design this system. Consider using the Model-View-Controller and DAO patterns.

Write an interface for a DAO that uses transfer objects to move claims and policies between the CPS and the database.

Posting Messages

Correspondents can post messages to each other using a post office. (Study the diagram carefully to see how this is done.) What design pattern can be used to notify correspondents that they have received a message? Redraw the UML diagram showing how you would use this pattern.

problem1

Refactoring

Redraw the following diagram to eliminate the logical problems it contains. You may introduce new classes, interfaces, and arrows as needed.

problem3

Refactoring 2

What design patterns can be used to improve the following design? Redraw the diagram showing how to apply these patterns.

problem2

 

Spread Sheet

A subscriber is anything that implements the Subscriber interface:

interface Subscriber { void update(); }

A. [5 points] Implement a simple Publisher class that uses this interface.

A spread sheet contains an array of cells. A cell contains a numeric value, a list of provider cells that it may depend upon, and an abstract computeValue operation that computes the value using the values in the provider cells. For example, the value of an averaging cell is the average of the values of its provider cells. The computeValue operation must be called each time any of the provider cells changes its value. Of course any cell may be a provider for other cells.

B. Draw a UML class diagram showing how you would use the Publisher-Subscriber pattern could be used to design this spread sheet.

C. Implement the Cell and AveragingCell classes using your Publisher class and Subscriber interface from above.

Job Processing

A job can be a task or a process. A job can be performed. A task is a simple job that has no steps. A process is a job that contains a sequence of jobs that must be performed in a specific order.

A. Draw a class diagram showing how you would model jobs using the Composite Design Pattern.

B. Translate your model into Java. . Be sure to provide the necessary bookkeeping methods. (You can leave out the details of performing a task.)

Java Calculator

A function calculator allows users to enter a number in a text field. When the "result =" button is clicked, a label displays the result of applying some function to the user's input. Currently, the function is the square function:

A. Assuming a main method that creates and shows the locator is provided in another class, implement the CalculatorPanel class. Use the Strategy Design pattern to allow different calculator panels to work with different functions.

B. Following the Adapter pattern, implement an adapter that turns Java's Math.sin method into a strategy that can be used with your calculator.

Monsters Inc.

A Monster class in a computer game implements the IMonster interface:

class Monster implements IMonster {
   void attach(Hero h) { /* crush h */ }
}

To make things more interesting, some monsters might flame the hero before crushing him. Others might bite before crushing. Still other might flame and bite before crushing.

A. Draw a class diagram to show how you would allow users to enhance the behavior of monsters using the Decorator pattern.

B. Repeat part A. using the Strategy design pattern.

Meta Modeling

A program is a sequence of one or more commands. There are three types of commands: if-commands, while-commands, and expressions. An if-command has the form

if (EXPRESSION) COMMAND [else COMMAND]?

where the question mark indicates that the else part is optional. A while-command has the form:

while (EXPRESSION) COMMAND.

Everything else is an expression.

Draw a UML class diagram showing how the concepts mentioned are related.

Files

A file contains a list of bytes:

List<Byte> contents;

There are text files and binary files. There are Unix files and Window files. These files provide the ability to write a single byte.

Draw a class diagram showing how you would use the Bridge pattern to separate conceptual files (text and binary) from file implementations (Unix and Windows).