Part 3 of 6 · 1 min read
Classes
Who owns each rule.
| Class | Kind | Job in one sentence |
|---|---|---|
Book |
value (record) | ISBN, title and author |
Copy |
entity | One physical copy, with an id and whether it is on loan |
Member |
value (record) | An id and a name |
Loan |
entity | A copy lent to a member, with due date, renewals and return date |
FinePolicy |
interface | Turns a due date and a return date into a fine |
PerDayFine |
class | A fine per late day with a cap |
Clock |
interface | Supplies today's date (Level 5, module 5) |
Library |
entity | Holds copies, members, loans, holds and unpaid fines, and applies the rules |
LibraryException |
checked exception | A refusal that the caller can act on |
Where each rule lives:
- A
Loanknows its own dates, so it renews and closes itself. It does not know the loan limit or the queue. - The limits, the queue, the fines and the order of checks need to see many loans and holds, so
Libraryowns them. - The fine amount is a business rule that changes, so it is behind
FinePolicy(Level 4, module 1). - Today's date is behind
Clock, so a test can move forward 20 days in one step.
The service holds its data in maps so lookups are quick:
Library has-many Copy (by id)
Library has-many Member (by id)
Library has-many Loan (open and closed, for history)
Library has-many hold queue (title ISBN -> queue of Member)
Library has-many unpaid fines (member id -> amount)
Loan refers to Copy, Member
Copy refers to Book
Check yourself: 1. Which class renews a loan, and which class decides whether it may? 2. Why is the clock injected? 3. Why is a loan kept after it closes?