Grade 12 Computer Science · Designing classes for a problem
From the description to the classes
A starting point that works, and why it is only a starting point.
The important class that is not a thing at all
“A system managing the lending of books in a library”: one sentence, and now it must become classes. Book and reader come to mind at once. But the class most needed has a date, a due date and a mark of whether it was returned, and it is neither a book nor a reader. What is it, and how do you not miss it?
One receives a description of a problem in words — “a system managing the lending of books in a library” — and must get from it to classes. The question “where does one begin” looks entirely open, and it has a practical starting point that works nearly always.
// "a system managing the lending of books in a library"
//
// nouns: system, lending, book, library
// verbs: manage, lend, return, search, compute a fine
//
// first pass: Book, Reader, Loan, Library
// Loan is a noun that is easy to miss and hard to do without.The noun most often missed
There is one kind of noun almost always forgotten, and it is usually the most important: the relationship between two things. A loan is not a book and is not a reader — it is the relationship between them, and it has data of its own: a date, a due date, whether it was returned. Whoever does not create a class for it is forced to squeeze that data into the book or the reader, and both are the wrong place. The sign identifying this: every time there is a datum belonging to a pair and not to either of them, there is a missing class there.
| In the description | A candidate for… |
|---|---|
| A noun | A class |
| A verb | An operation |
| A datum belonging to a pair | A missing class |
Example 1 — why this is only a starting point
- Some of the nouns are fields.
- And some of the verbs belong to one class.
- But there is something to start from.
Example 2 — why the loan needs a class
- It has data of its own.
- A date, a due date, whether it was returned.
- And they belong to the pair and not to either.