From a paragraph to a list of classes
After this: Read a requirement paragraph, ask what it leaves open, and sort its nouns into classes, values and things outside.
On this page · 4 sections · 3 checks
A friend runs a small library from a cupboard at work, and the list of who has which book lives in a notebook. They ask you to write a program instead. Before you write a line, you need to know what the program must do. This lesson turns a short description into a list of classes, the same way you planned the parking lot in "Design a parking lot", only faster.
Ask before you draw
Your friend's first words are "people borrow books and bring them back". That leaves a lot open. A good first step is to ask questions until the rules are clear:
- How many books may one person have at once?
- How long is a loan, and what happens when a book comes back late?
- Can the library have more than one copy of the same book?
- What if every copy is out and someone wants it? Can they wait for it?
- What can people search by: the title, the author, both?
Each answer is a : a rule the program must follow. Rules about what the program does, such as "a loan lasts 14 days", are called functional requirements. Rules about how well it does it, such as "a search answers in under a second", are non-functional. A low-level design round is mostly about the first kind.
Here is the paragraph you and your friend agree on. Every lesson in this tutorial builds from it.
Members borrow copies of books. A book has an ISBN, a title and an author, and the library can have several copies of it. A member can have at most 3 books on loan. A loan lasts 14 days, and each day late costs 5 rupees. Members can search the catalogue by a word from the title or the author. When every copy of a book is out, a member can reserve it and joins a line; when a copy comes back, it is held for the first member in the line, who is told it is ready.
It is as useful to agree what is not in the program. Here, that is paying fines, a hold that runs out if nobody collects the book, a limit on how much a member can owe, and a website. Saying this out loud keeps the design small enough to finish.
Nouns become classes
Now read the paragraph again and pick out the nouns, the names of things. Each one becomes one of three kinds:
| Noun | Becomes | Why |
|---|---|---|
| library | class Library | the system itself; it keeps the books, copies, loans and lines |
| book | class Book | an ISBN, a title and an author that belong together |
| copy | class BookCopy | each copy can be on the shelf or out, on its own |
| member | class Member | has a name, loans and fines, and can be told things |
| loan | class Loan | ties one copy to one member, with a due day |
| ISBN, title, author | fields of Book | pieces of text with no rules of their own |
| days, rupees, the limit of 3 | numbers (int) | values the classes keep or compare |
| catalogue | the library's books | another name for something already on the list |
| line | kept by Library | one line of waiting members for each book |
A noun that becomes a class is often called an : something the system keeps track of over time. A noun that is only a number or a piece of text is a value, and it becomes a field.
One class is not in the paragraph at all. The fine is a rule that may change ("2 rupees a day for students"), so it gets its own small class, FinePolicy. Lesson 3 explains why.
Verbs become methods
The verbs, the doing words, become methods. Ask which class owns the data each verb changes:
| Verb in the paragraph | Method |
|---|---|
| borrow | borrow on Library |
| bring back (comes back) | return_copyreturnCopy on Library |
| search | search on Library |
| reserve, join a line | reserve on Library |
| costs (a fine) | fine on FinePolicy |
| is told it is ready | update on Member |
Most methods sit on Library because the library sees every copy, loan and line at once. The member only needs a way to be told something.
Check yourself
Which noun from the library paragraph becomes a value (a field holding a number or some text), not a class of its own?
Before the paragraph was agreed, you could ask only one question about the library. Which one changes the classes and the code the most?
The paragraph says "a member can reserve it and joins a line". Which method does this verb become, and on which class?
- Ask about the rules before drawing: limits, loan length, fines, what happens when every copy is out, and what search matches.
- Say what is out of scope, such as paying fines or a hold that runs out, to keep the design small enough to finish.
- Sort the nouns of a requirement paragraph into classes (entities), values (fields) and names for things already on the list.
- Turn the verbs into methods on the class that owns the data each verb changes.