Design the software for a library. Members search for books, borrow copies, return them and pay a fine when they are late. Librarians add books and copies.
A **book** can have many physical **copies**; a member borrows a copy, not the book.
Members can **search** by title, author or subject.
A member can hold at most **5 loans** at once, each for **14 days**.
A late return produces a **fine**; the rule for the fine must be **changeable**.
A member can **reserve** a book whose copies are all out and is told when one returns.
Stage 1: Books, copies and members
Separate the title from the physical copy.
The requirements are a list of nouns. They are not yet a model of who owns what. `Book` is the title, `BookCopy` is one physical item, and `Loan` ties a copy to a member with a due date. Because the copy is its own class, two copies of one title are tracked apart and one can be out while the other is on the shelf. Splitting book and copy adds a class, but merging them breaks the first time a library owns two of the same book.
Stage 2: A catalogue and the library
Give search and lending a home.
Nothing owns searching, and nothing enforces the 5-loan limit or the 14-day period. `Catalog` owns the books and search. `Library` is the entry point: `checkout` checks the limit, picks a free copy and creates the `Loan`. Each class has one job: the catalogue finds things, the library applies the rules. Callers talk to `Library` only. `Library` can grow into a god class. Keep it as coordination and let each rule live in a small class of its own.
Stage 3: Fines change: Strategy
Make the late rule swappable.
The fine is a formula that sooner or later gets a student discount or a cap. A `FineStrategy` interface with `amount(daysLate)`. `PerDayFine` and `CappedFine` implement it, and `Library` holds one. Adding a rule is one new class and one line where the library is built (**Strategy**). Lending code never changes. Two classes for what could be an `if`. That pays off at the second rule, not the first.
Stage 4: Reservations and notifying
Tell the next member when a copy returns.
A member who wants a borrowed book has no way to wait for it. A `Reservation` records who waits. When a copy returns, `Library` calls every `ReturnListener`; `EmailNotifier` tells the first person in line. Lending does not know about email. A text message is one more listener (**Observer**). Listeners run inside the return call. If sending is slow, queue it so a return never waits on an email server.