Learn

Architecture & pattern guides

Read the idea, build it on a Grid, prove it in a challenge. Short, practical guides — illustrated with RodGrid's own nodes — on system design, design patterns, flow modeling, and how to get the most out of the app.

All articles

The class box: name, attributes, operations

The three compartments of a UML class, how visibility, types and operation signatures are written in them, and when a set of values becomes an Enumeration.

Three questions, top to bottom

A UML class box answers three questions, top to bottom: what is it called, what does it know, and what can it do. The name compartment carries the type name (and a keyword in guillemets, like «interface», when it needs one), the middle holds attributes, and the bottom holds operations. A box with only a name is not wrong: it is a class you have not decided anything about yet, or one whose members this view leaves out.

The Parking Lot Challenge starts with boxes like that. Its brief mentions vehicles, parking spots, spot sizes, tickets and a parking rate, and a first draft usually puts one named box on the Grid for each. Take the spot and ask the two remaining questions. A spot knows its size and whether a vehicle is in it right now. It can tell you whether a given vehicle fits, and it can take one in.

One line per member

Each member is written as one line of shorthand. Visibility is a prefix: + public, - private, # protected. Types go after a colon. So - occupied: bool is a private attribute, and + fits(vehicle: Vehicle): bool is a public operation that takes a vehicle and answers yes or no.

A parking spot, filled in

The middle compartment is what the spot knows; the bottom is what it does with that. Its size is a SpotSize, not free text.

The parameters and the return type are often where the design is. + assign() only says a spot can be assigned. + assign(vehicle: Vehicle): Ticket says the spot is told which vehicle arrived, and that the spot is the one that hands back the ticket. A reader can now disagree with that choice, which is the reason to draw it. The brief's question about who owns the parking-rate calculation is settled the same way: by the box whose bottom compartment holds that operation, with what it takes and what it returns.

A size is not a string

The size could have been - size: text, with a comment saying the legal values are compact, regular and large. Nothing in the model would then stop "medium" from turning up. An Enumeration is a class box with a fixed set of values in place of attributes: SpotSize lists its three values in the middle compartment and leaves the bottom empty, and - size: SpotSize now says what the comment used to say. The enum turns a comment into part of the model.

The diagram draws no line from the spot to SpotSize, because the attribute's type already names it. Association, aggregation, composition explains which lines to draw between boxes and what each kind claims.

Types nobody creates directly

The brief's Vehicle is a base type: nobody parks a plain vehicle, only one of its concrete kinds. If a type is never instantiated itself — it only declares what others must provide — mark it «interface» or use the Abstract Class stencil; a plain Class tells the reader it can have instances of its own. Inheritance, interfaces and abstract types explains which of the two to choose.

The Object stencil is a class box as well, for an instance rather than a type; Object diagrams: one moment of a running system shows when one is worth drawing.

NoteOn a Challenge Grid a class has only its name: the Inspector that fills the attribute and operation compartments is hidden. Write the members the brief asks for in the name instead — double-click the node and type, for example, ParkingSpot (size, occupied) or Ticket (entryAt, exitAt). On a Diagram Grid, keep using the compartments.

What a box should leave out

  • Trivial getters and setters as operations. + getSize() and + setOccupied(value) add little that the attributes did not already say, and they bury fits and assign, the two lines that tell the reader what a spot does.
  • Every column of the database row in the middle compartment. A Ticket listing createdAt, updatedAt and rowVersion reads like a schema dump. A class diagram is a design document: model the attributes the design turns on, such as the entry and exit times a fee is computed from.

Fill the boxes on Parking Lot

On the Parking Lot Challenge, the first draft is the one this article started from: one box per noun in the brief. Give the spot its size and occupied state, make the size an Enumeration, and decide which box owns the rate calculation, writing that operation in its name with what it takes and returns, for example fee(exitAt): Money. Before you submit, look again at any box with no members, no relationship of any kind, and no attribute that uses it as a type. If you cannot name the role it plays in the design, remove it: it was probably a word in the brief, not a type.

Practice this