The class box
A class is a three-part box: name on top, attributes (fields) in the middle, operations (methods) at the bottom. Visibility markers prefix each member:
+public-private#protected~package- Italic class names or methods mean abstract; underlined members mean static.
The relationships (the arrows)
Patterns are mostly about how classes relate, so the arrows matter more than the boxes. These six cover almost everything you'll see:
| Relationship | Notation | Means | Java |
|---|---|---|---|
| Inheritance (generalization) | solid line, hollow triangle ▷ | "is a" | class Dog extends Animal |
| Realization | dashed line, hollow triangle ◁┄ | "implements an interface" | class Dog implements Pet |
| Association | solid line → | "knows / uses long-term" | a field referencing another object |
| Aggregation | hollow diamond ◇— | "has a" (shared, can outlive) | a Team holds Players passed in |
| Composition | filled diamond ◆— | "owns" (part dies with whole) | a House creates its Rooms |
| Dependency | dashed arrow ┄> | "uses temporarily" | a method parameter or local var |
Reading it back as Java
Here's a small diagram described in code. A Car realizes Vehicle, is composed of an Engine, and depends on a Driver only inside one method:
interface Vehicle { void start(); } // realization target
class Engine { void ignite() {} }
class Car implements Vehicle { // ──▷ realization (dashed, hollow triangle)
private final Engine engine = new Engine(); // ◆── composition: Car owns its Engine
public void start() {
engine.ignite();
}
void drive(Driver driver) { // ┄> dependency: used only here, as a parameter
driver.steer(this);
}
}You rarely need a tool. A whiteboard sketch with boxes and the right arrow is enough to communicate a design. The arrow you'll draw most in patterns is realization — "this class implements that interface."
- Confusing aggregation (parts can exist independently) with composition (parts die with the whole). When in doubt, ask "if I delete the whole, does the part still make sense?"
- Drawing an association (a stored field) when you really mean a dependency (a passing parameter). Dependencies are weaker — prefer them.
A UML class box shows name, attributes, and operations with visibility markers (+ - #). Six relationships carry the design: inheritance and realization (is-a / implements), association/aggregation/composition (has-a, in increasing strength of ownership), and dependency (uses temporarily). Read the arrows first — that's where patterns live.