Object Diagrams: Snapshots of a Running System
A class diagram describes every configuration your system is allowed to reach: which classes exist, which attributes they carry and how many objects may be connected. It never shows any particular configuration. Sometimes you need to see one: the state of memory at a breakpoint, the data a unit test sets up, or an example that makes a tricky association concrete. For that you draw an object diagram. This lesson covers how to read and draw one, and how to check it against the class diagram it comes from.
Reading object notation
An object diagram shows specific objects, the values held by their attributes, and the connections between them at a single moment. In UML each object is a box, formally an instance specification, drawn with these conventions:
- The header is objectName : ClassName, underlined. The underline tells a reader that this box is an instance, not a class.
- An anonymous object omits the name and keeps the colon, as in : LineItem. You may instead omit the class when it is obvious, leaving just the underlined name.
- The lower compartment holds slots written attribute = value, such as total = 59.50. Show only the slots that matter for the point you are making.
- There is no operations compartment. Every object of a class shares the same operations, so listing them says nothing about this snapshot.
A link is an instance of an association: a line joining two particular objects. It may carry the association name or role names, but it always connects exactly one object to exactly one other.
Checking a snapshot against its class diagram
Every element of an object diagram must be an instance of something in the class diagram.
Class diagram element | Object diagram counterpart |
|---|---|
Class Order | Object o1 : Order, underlined |
Attribute total : Decimal | Slot total = 59.50 |
Association with multiplicities | Link between two objects, no multiplicities |
Generalization | Not drawn; an object names its most specific class |
Operation | Not shown |
Multiplicity is where snapshots most often go wrong, and checking it is a counting exercise. Take an association between classes A and B whose B end has multiplicity m..n. Let ka be the number of links from object a to B objects, and jb the number of links at each B object. Then
where L is the total number of links of that association. Counting from both ends must give the same L, which is a quick check on your own tally. A snapshot conforms only if every object satisfies the multiplicity at every association end it takes part in.
Example: Suppose the class diagram behind the figure says each Order contains 1..∗ LineItems and each LineItem belongs to exactly 1 Order. Object o1 has 2 links to LineItems, which lies inside 1..∗, and each LineItem has exactly 1 link to an Order, so L=2 counted from either end. The slots tell a consistent story too: 2 × 19.75 + 20.00 = 59.50.
When an object diagram earns its place
- Explaining a tricky class diagram. A recursive association such as Employee manages Employee is easier to grasp when you show ceo, two managers and their reports as a concrete tree.
- Designing test fixtures. The objects a unit test creates before it acts form an object diagram; drawing it first exposes missing links.
- Debugging. A sketch of the heap at a breakpoint reveals aliasing, such as two orders wrongly sharing one LineItem.
- Finding counterexamples. If you can draw a snapshot that conforms to the class diagram yet makes no business sense, the multiplicities are too loose.
An object diagram has no time axis. It cannot show which object calls which, or in what order; that is the job of interaction diagrams.
What to remember
- An object diagram is a snapshot: specific objects, slot values and links at one moment.
- Object headers are underlined name : Class; anonymous objects keep the colon.
- Links are instances of associations and carry no multiplicities.
- A snapshot is valid only if every object meets every multiplicity bound, lower and upper.
- Counting links from both ends of an association must give the same total.
Example: Validating a library snapshot
A library system's class diagram has two associations:
- Member to Loan: each Loan belongs to exactly 1 Member, and each Member holds 0..3 active Loans.
- Loan to BookCopy: each Loan refers to exactly 1 BookCopy, and each BookCopy is on 0..1 active Loans.
A developer captures an object diagram at a breakpoint. The links it shows are summarised below; in the diagram itself every object name is underlined.
Member object | Loan objects linked | BookCopy linked to each loan |
|---|---|---|
m1 : Member | L1, L2 | c1, c2 |
m2 : Member | L3, L4, L5 | c3, c4, c5 |
m3 : Member | none | none |
A sixth copy, c6 : BookCopy, appears in the snapshot with no links.
a) Count the objects in the snapshot and the links of each association.
Count objects class by class: 3 Members, ________ Loans and ________ BookCopies, so the snapshot holds 3 + 5 + 6 = 14 objects.
Count Member to Loan links from the Member end: m1 has ________ links, m2 has ________ links and m3 has 0, giving 5 links. Check from the Loan end: each of the 5 Loans has one Member link, so the total is again 5.
Each of the 5 Loans has one BookCopy link, so there are 5 Loan to BookCopy links, and 10 links in total.
b) Does the snapshot conform to the class diagram?
Check each association end in turn.
- Member end of Member to Loan, exactly 1: each of L1 to L5 has one Member link. Satisfied.
- Loan end of Member to Loan, 0..3: m1 has 2, m2 has 3, which sits exactly on the upper bound and is allowed, and m3 has 0, allowed because the lower bound is 0. Satisfied.
- BookCopy end of Loan to BookCopy, exactly 1: each Loan has one copy. Satisfied.
- Loan end of Loan to BookCopy, 0..1: copies c1 to c5 have 1 Loan each and c6 has ________. Satisfied.
Every object meets every bound, so the snapshot is valid.
c) Member m2 asks to borrow c6. The proposed new snapshot adds L6 : Loan linked to m2 and to c6. Does it conform? If the library instead lets m3 borrow c6 through L6, how many objects and links does the resulting snapshot contain?
With the proposed change, m2 would hold ________ Loans. That exceeds the upper bound of 0..3 at the Loan end, so the proposed snapshot does not conform. The other ends are fine: L6 has one Member and one copy, and c6 would have one Loan, within 0..1. The system should refuse this loan.
If m3 borrows c6 instead, m3 holds 1 Loan, within 0..3, and every other count is unchanged, so the snapshot is valid. Adding L6 brings the objects to 14 + 1 = 15, and its two links bring the links to 10 + 2 = 12.
A developer is sketching the state of a web shop at a breakpoint. One object of class ShoppingCart is named cart42, and its itemCount attribute currently holds 3. Which header and slot follow UML object diagram notation?
A monitoring application's UML object diagram shows selected slots for three Sensor instances. Each sensor has an optional location attribute and a samples collection. The notation null explicitly records no value for location, while {} explicitly records an empty samples collection. Slots may be omitted from this view, and no other information is available.
The diagram's displayed information is transcribed below:
- north : Sensor displays location = "roof" and samples = {19, 21}.
- south : Sensor displays samples = {}; its location slot is omitted.
- spare : Sensor displays location = null; its samples slot is omitted.
Classify each claim using exactly supported, contradicted, or undetermined. A supported claim follows from the displayed information; a contradicted claim conflicts with it; an undetermined claim cannot be settled from it.
| Claim | Classification |
| The north sensor has exactly two sample values. | |
| The south sensor has at least one sample value. | |
| The south sensor has no location value. | |
| The spare sensor has no location value. | |
| The spare sensor has an empty samples collection. |