Copyright © 2012 David Schmidt

Lecture 4:
Class diagrams




An object diagram is a "blueprint" of how storage is managed when a system executes. It is similar to the "traffic diagram" an architect makes of a building that shows the movement of people through the building while the building is used.

But an architect also makes a "floor plan" of the building, which shows the layout of rooms, halls, and stairs that will be constructed for use.

A class diagram is a blueprint of the components needed to build an executing software system --- it is the "floor plan" of the software system. It is the most important blueprint form that we learn in this course.

UML class notation is used to draw a class diagram. We use the variant that is documented in the Visual Studio lecture notes. There is a related tool, Nclass, which helps you draw class diagrams and generate Visual Studio projects from them. Nclass was used to make the diagrams in this note. (There are more in the VS Notes.)

Example: Bank Database

At the end of the previous Note, there were examples of object diagrams. One was of a banking database. Here's a repeat of part of that system:

The object diagram shows how two "controller" objects "own" the multiple customer records (objects) and the multiple account records. Further, a customer record holds handles to the accounts that the customer owns.

When we program the database, we write classes that can generate the object diagram. This means we need at least classes for CustomerDatabase, BankDatabase, Customer, and Account. The classes will depend upon (reference) each other in the pattern shown in the object diagram.

A class diagram shows the classes that must be written and the dependencies. Here is a class diagram that defines the classes that must be coded to bring to life the database:


Additional detail --- field names, methods --- are added to indicate what must be coded to implement the database.

The meanings of boxes and arrows are documented in the Visual Studio lecture notes. Here is a quick summary:

The above format is standard and has been used for decades. But some people find the labels on the arrows confusing. When you use the Nclass tool to draw the diagrams, you can omit the labels if you declare the correct fields, like this:


One advantage of this alternate approach is that Nclass can generate your C# code for you from the class diagram you draw. (Of course, Nclass won't code the bodies of the methods, but it will generate everything else! This is a big payoff for drawing a class diagram before you start coding.)

Example: Exam Simulation

Say we are designing a computer simulation of students taking an exam. The students bring some blank paper, and some pencils and pens. At the exam, each student gets a question sheet; all the students share (well, look at) the big clock at the front of the room.

The computer simulation must construct objects for the entities just described; here is an object diagram of what the exam simulation might look like while it is executing:

The object diagram gives big clues about what classes must be written for the simulation. Here is a class diagram that could generate the above object diagram:

Exercise