Nouns become classes, verbs become methods
After this: Sort the nouns of the requirements into classes, values and things outside the system, and turn the verbs into methods.
On this page · 6 sections · 3 checks
Lesson 1 ended with an agreed paragraph. Now you need classes. Staring at a blank page and inventing them is hard. There is a simpler way to start: the paragraph already names most of them. The things it talks about are nouns, and the actions are verbs.
Mark the words
Here is the paragraph again. The nouns are in bold and the verbs that are actions are in italics:
A parking lot has several levels. Each level has spots of three sizes: small, medium and large. A bike needs a small spot, a car a medium spot and a truck a large spot; a vehicle may use a bigger spot when none of its own size is free. When a vehicle enters, the lot gives a ticket that records the vehicle, the spot and the entry time. When it leaves, the driver pays for every started hour: 10 rupees for a bike, 20 for a car and 50 for a truck. The lot can say how many spots of each size are free.
The verb "has" is not marked. "A lot has levels" is not an action: it is a link between two classes, a "has a" line in the diagram. Lesson 3 draws it.
Sort the nouns
Not every noun becomes a class. Ask one question of each: does the system need to keep track of this thing, with its own data that can change? Such a thing is called an , and an entity becomes a class. A noun that is only one number or one word becomes a field instead. A noun that the system never stores stays outside it.
| Noun | Becomes | Why |
|---|---|---|
| parking lot | class ParkingLot | The one object that drivers talk to. |
| level | class Level | It has a number and its own spots. |
| spot | class Spot | It has a name, a size, and a vehicle that comes and goes. |
| vehicle, bike, car, truck | abstract class Vehicle, with Bike, Car, Truck | Each kind has its own size and its own rate. |
| ticket | class Ticket | A new one is made each time a vehicle enters. |
| size | a value: Size, with three choices | Only three fixed values, and no data that changes. |
| entry time | a value: a whole number, a field of Ticket | One number. |
| hour, rupees | values: whole numbers | Used in working out the fee. |
| driver | outside the system | Uses the lot, but the program keeps nothing about drivers. |
Size gets its own type, with a fixed list of three named values. Such a type is called an . The values are SMALL, MEDIUM and LARGE, in that order, so the program can ask whether one size is smaller than another. Lesson 4 shows the code in both languages.
Turn the verbs into methods
Each action becomes a method on the class that has the data to do it:
| Verb | Method | On which class |
|---|---|---|
| enters | park | ParkingLot: it knows every level. |
| leaves | unpark | ParkingLot: it knows every ticket that is still open. |
| pays | fee | FeeCalculator, a new class; see below. |
| say how many are free | free_countfreeCount | ParkingLot: it can count across every level. |
| records | (no method) | It describes the fields of Ticket. |
The paragraph also needs actions it never names. To park, the lot must look for a spot on each level, ask each spot whether the vehicle fits, and put the vehicle there. Those are three more methods:
- find_spotfindSpot on
Level: search this level for the best free spot. fitsonSpot: is this spot free, and is it big enough for this vehicle?assignandreleaseonSpot: a vehicle takes the spot, and later leaves it.
A class the paragraph does not name
"Pays for every started hour" is a rule, and rules about money change. A weekend rate or a discount for trucks are likely next requests. So the fee rule gets a class of its own, FeeCalculator, even though no noun in the paragraph names it. Then a new rule means a new fee class, and the lot itself does not change. You met this idea in "SOLID, one principle at a time" (one reason to change per class) and in "Three patterns you will meet first" (Strategy). Lesson 3 shows where it sits in the drawing.
Check yourself
"Entry time" is a noun in the paragraph. What does it become in the design?
Which verb in the paragraph became the lot's unpark method?
The driver is a noun in the paragraph, but the design has no Driver class. Why?
Where this method stops
- Mark the nouns and verbs of the requirement paragraph: nouns are candidate classes, verbs are candidate methods.
- Make a noun a class when the system keeps track of it with data of its own (an entity); make it a field when it is one value; leave it out when the system stores nothing about it.
- Use an enum for a fixed list of named values, such as the three spot sizes.
- Put each method on the class that has the data to do it, and add the methods the paragraph implies, such as finding a spot.
- Give a rule that is likely to change, such as the fee, a class of its own even when no noun names it.