Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 39 Β· Class and Object Diagrams

Object Diagrams: Snapshots of a Running System

Use UML object diagrams to show concrete instances and links at one moment in time, validate a class diagram with examples, and debug a design with real data.

A class diagram describes everything that could exist. An object diagram describes what does exist at one moment: specific instances with real attribute values, connected by links. Think of the class diagram as the blueprint and the object diagram as a photograph of a finished building.

Notation

An object is a rectangle whose name is underlined and written name : Class. Attributes show current values. A link between objects is a solid line, and it must be an instance of an association from the class diagram.

Form Example Meaning
Named object alice : Customer an object called alice of class Customer
Anonymous object : Customer some Customer; the name does not matter
Class only alice name given, class omitted (when obvious)
Attribute value name = "Alice" current state of an attribute

Mermaid has no dedicated object-diagram syntax. A common approach is to use classDiagram with labelled boxes. We label each box name : Class and list values as members. In UML proper, the name would be underlined.

classDiagram
    class alice["alice : Customer"] {
        name = Alice
        email = [email protected]
    }
    class order1["order1 : Order"] {
        number = 1001
        status = PAID
    }
    class line1["line1 : OrderLine"] {
        quantity = 2
        unitPrice = 9.50
    }
    alice --> order1 : places
    order1 *-- line1

One concrete Customer, her Order and one OrderLine.

From class diagram to object diagram

The class diagram said Customer 1 --> 0..* Order. The object diagram shows one customer linked to one actual order. Each object must satisfy the class diagram:

  • Its class exists, and the attributes match.
  • Each link instantiates an association with the right multiplicity.
  • Abstract classes are never instantiated (so no object of Shape, only of Circle).
  • A composed part has exactly one owner.

Because of those rules, an object diagram is the quickest way to test a class diagram. Invent a realistic example and try to draw it. If you cannot, or you need a link the model forbids, the model has a flaw.

Python Check an object snapshot against multiplicities

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two violations are exactly what an object diagram would reveal. Either the data is wrong, or the class diagram's multiplicities are (for example, guest checkout needs 0..1).

Showing a state at a moment

An object diagram represents one instant. To tell a story across time, draw two diagrams (before and after) or switch to a sequence diagram. Typical uses:

  • Explaining a design to someone: "here is what the data looks like for a typical order".
  • Documenting a tricky structure: a tree, a linked list or a graph is much clearer with sample nodes.
  • Debugging: draw what the objects actually contain when a bug occurs, then compare to what the model says.
  • Testing the model: write down the examples that become test data.

A good example is a small tree.

classDiagram
    class root["root : Folder"] {
        name = docs
    }
    class sub["sub : Folder"] {
        name = uml
    }
    class f1["f1 : File"] {
        name = notes.txt
    }
    class f2["f2 : File"] {
        name = todo.txt
    }
    root *-- sub
    root *-- f2
    sub *-- f1

A Folder tree: one root folder containing a subfolder and a file; the subfolder holds one more file.

Python Build the same snapshot as live objects and print it

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The printed outline is a textual object diagram of the structure that the class diagram (Folder contains 0..* of Folder or File) describes in general.

UML calls each box an instance specification. Some further points:

  • You can show only the attributes that matter for the point you are making.
  • A link can carry a role name and, for an association class, the link's own data.
  • Several objects of the same class can appear; a multiobject (stacked rectangles) stands for a set.
  • A classifier may be omitted if the diagram is small and unambiguous.

Class vs object diagram

Class diagram All possible structures.
Object diagram One real snapshot.
Use it to test Does the model allow my example?

Instances show as name : Class.

1 / 2

Writing a good object diagram

  1. Choose a scenario with a purpose ("an order with two lines, paid by voucher").
  2. Draw the minimum objects needed.
  3. Give them realistic names and values, not a1, b2.
  4. Check each link against the class diagram.
  5. Note anything the class diagram could not express, and fix the model.

Object diagrams are cheap. Whenever a class diagram makes you uneasy, draw one on paper with real names. Ten minutes there saves hours of debugging a flawed design.

Common mistakes

  • Forgetting the underline (or the name : Class form) so the box looks like a class.
  • Drawing classes in an object diagram. Every box must be an instance.
  • Showing a link with no matching association in the class diagram.
  • Too many objects. Five to ten is plenty; more becomes a database dump.

Check your understanding

Quick check Β· 4 questions

of 4 answered You scored / 4 Β· perfect!

  1. 1. What does an object diagram show?

    It is a snapshot of concrete objects with values, instantiating a class diagram.
  2. 2. How is an object's name written?

    Objects are labelled name : Class, underlined in UML.
  3. 3. Why can you not have an object of an abstract class in the diagram?

    Only concrete classes can have instances.
  4. 4. How can an object diagram help you find design flaws?

    Realistic examples reveal missing optional links, wrong multiplicities or missing classes.

Summary

  • An object diagram is a snapshot of instances (name : Class) and links at one moment.
  • Every object and link must be allowed by the class diagram.
  • Use object diagrams to explain, to document tricky structures, to debug and to test a model.
  • Keep them small, realistic and focused on a scenario.
  • Mermaid draws them with labelled class boxes; UML underlines the names.

Next: Use Case Basics