Archive‎ > ‎Fall 2009‎ > ‎

Class Diagram

The class diagram shows the main classes in your program, their fields and methods, and the way classes interact.

Class representation


Classes are represented as rectangles. The rectangle for a class is plit in three. The top part contains the name of the class, the middle part shows the fields of teh class, and the lower part lists the methods of the respective class.

For representing the visibility attribute of the class' members, the following notations apply:
+ public visible to all elements that can access the contents of the namespace that owns it.
# protected visible to elements that have a generalization relationship to the namespace that owns it.
private only visible inside the namespace that owns it.

If a method of a class is abstract, than its name is written in italics. Also the name of the class must be written in italics.

Interface Representation



An interface has only public methods, thus specifying visibility attributes is redundant.


Basic Interactions

Aggregation.


Aggregation represents the "has-a" relationship. It is an association that represents a part-whole or part-of relationship.
In the example, the SwEng class has zero or more (0..*) Students.

Inheritance


Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality.
In this example, class SwEng extends class Course.

Dependency


Dependency is a weaker form of relationship which indicates that one class depends on another because it uses it at some point of time. Dependency exists if a class is a parameter or a local variable of a method of another class.
In this example, one of Order's methods creates new Payment objects.

Multiplicity

The multiplicity of an association end is the number of possible instances of the class associated with a single instance of the other end (for those of you familiar with ER diagrams in databases, this should sound familiar). Multiplicities are single numbers or ranges of numbers. Common multiplicities are:
0..1 No instances, or one instance (optional, may)
1 Exactly one instance
0..* or * Zero or more instances
1..* One or more instances (at least one)

Examples:

Designing a SmartCopier.


A classic example of how to use aggregation and inheritance. The Copy interface uses a Reader and a Writer to copy from the source to the destination. The Reader interface is implemented by KeyboardReader, while the Writer interface is implemented by two classes: KeyboardWriter and DiskWriter.

Designing a Media Player.


The main class of the Media Player program is MediaCollection. It has a GUI class used to display information about the collection the user has.
Each file in the collection implements the Media interface. Our media player can play MP3 music files and AVI and WMV video files.

Designing a Program Executor.

The Program Executor program works as follows. The user uses the GUI to type in a program (s)he wants to execute. The GUI has a single ProgramExecutor object. The ProgramExecutor is passed the name of the program to be executed. However, there are multiple types of programs in the system: GUIProgram, CommandLineProgram and RemoteProgram. Each of them has another ProgramExecutor as its successor.
If the current ProgramExecutor can run the specified program, it does so. If it cannot, it requests its parent to try to run the program.

Designing a Document Viewer.

The main class of the program is Document. A Document might contain multiple Graphics elements, like Pictures or Films. However, loading the pictures and the films before they are displayed increases memory consumption without any benefit. To remedy this, we have PictureProxy and FilmProxy which contain limited information about the Graphics they are going to display, like information needed for correct paging of the Document. Since both PictureProxy and FilmProxy implement the Picture, respectively the Film interface, they are as good as the real thing.

Designing a GUI hierarchy.


This is the Composite Design Pattern. It represents the basic design pattern used to implement the GUIs. Composite allows a group of objects to be treated in the same way as a single instance of an object. The intent of composite is to "compose" objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions uniformly, much like a GUI.

Designing an OS Operation Listener


Say we want to perform a particular computation when an OS event has taken place, like writing or reading from a file.
To do this we need to register a Listener with the OS for the particular event we are interested in. 
When the OS knows that a Read or Write operation has been performed, it invokes the Listeners that have registered for the respective event.
In our case, we have a WriteListener that is invoked when a file has been written.
When the WriteListener is invoked, it calls, in turn, all of its Command objects. Such a Command object is FilePrinter which, as the name suggests, will print the newly written file.