Card 05/ 09

ConceptDifficulty: Beginner2 min

Reading a Class Diagram: Boxes and Four Arrows

Inherits, implements, holds and uses: enough UML to follow every diagram on SOLID.

A class diagram is a drawing of classes and the relationships between them. It comes from UML, the Unified Modelling Language, a standard set of drawing conventions for software. This card teaches the small part of it this subject uses: a box for a class, a marked box for an interface, and four kinds of arrow. With those you can read every diagram on SOLID.

Each box is one class. Its name sits at the top, and the lines below list fields and then methods. A plus sign means public. An interface gets the label <<interface>> above its name, and it lists methods only, because an interface says what can be done without saying how.

The four arrows are what carry meaning. Each one says how one class depends on another, which is exactly what coupling is about. The diagram below shows all four, using the checkout from this topic split into parts.

The four relationships used in this subject

Read each arrow from the class without the arrowhead towards the class with it.

The four arrows, what they mean, and the Python each one stands for
ArrowNameMeansIn Python
Solid line, hollow triangleInheritsThe class is a subclass of the other.class InStoreCheckout(Checkout):
Dashed line, hollow triangleImplementsThe class provides every method of an interface.class EmailSender(Sender): where Sender is abstract
Solid line, open arrowheadHoldsThe class keeps a reference to the other in a field.self.store = store in __init__
Dashed line, open arrowheadUsesThe class needs the other only inside a method, briefly.Receipt(items).text() inside a method

The solid lines are the stronger ties. Inheriting or holding a reference lasts as long as the object does, while using something inside one method is a passing dependency. When a SOLID diagram shows a fix, look at how the arrows move: a solid arrow to a concrete class is often replaced by a dashed arrow to an interface.