Part 5 of 6 · 3 min read
Edge cases and changes
Queues, limits and scale.
Step 5: edge cases, tests and change requests
Edge cases:
| Case | Behaviour |
|---|---|
| Unknown member or copy id | Unchecked exception naming the id |
| Borrow with unpaid fines | Refused, with the amount |
| Member already waiting asks again | Not added twice |
| Copy returned while a queue exists | Stays free, but only the first in line may take it |
| Renew on the due date | Allowed, since "overdue" means strictly after |
| Return on the due date | No fine |
| Two members borrow the last copy at once | A race if not synchronized (see below) |
One real weakness: borrow is not thread-safe. Two requests for the last copy can both see it free. Add synchronized to the public methods of Library, or lock per title, as in Level 5, module 3. In a round, say it, since the question usually has more than one desk.
A second weakness is speed. openLoans and openLoanOf scan the whole loan list. For a small library that is fine. For a large one, keep a map from copy id to its open loan, and a map from member id to a list of open loans. Say what you would index and why, and do not build it unless asked.
A third is the hold that never gets used. If the first member in the queue never comes, the copy is blocked for everyone. A real system gives each hold a pickup deadline, after which it passes to the next member.
Tests to write first: each row of the run table; the queue order with three members; a renewal blocked by a waiting member; the fine on the due date and one day later; the cap; and a thread test for the last copy.
Change requests and the part that changes:
- "Different loan periods for different members." A
LoanPolicyreturned by member type (strategy).Librarystops holding the constants. - "Fines for damaged books." A new fine type. The unpaid map keeps working, and a
Finerecord with a reason could replace the plain number. - "Search by ISBN, by author, by year." A
SearchCriteriainterface with one class for each (Level 4, module 1), or an index map for speed. - "Notify a member when a hold is ready." An observer on the return event (Level 4, module 2).
- "Reference books that cannot leave." A flag on
Copyor a subclass with a rule that refuses to lend. The flag is simpler. - "Several branches." A
Branchthat owns copies. Holds then span branches, which is a bigger change, and it is fair to say so. - "Staff can override a limit." An
Actorwith a role, checked at the start ofborrow.
Common mistake
One Book class that holds the title and also whether it is on loan. It cannot say "three copies, one free", and holds have no title to attach to. Split the title from the copy first.
Interview angle
Answer in your head first, then open it.
How do you model a book and its copies?
A title as a value, and a copy as an entity that refers to it. Holds attach to titles and loans attach to copies.
Where do the limits live?
In the library service, since it sees all loans. A loan only knows its own dates.
How do you keep the hold queue fair?
A first-in first-out queue per title, and a copy is only lent to the head of the queue while a queue exists.
How do you change the fine rule?
The fine is a strategy that is injected. A new rule is a new class.
Check yourself:
1. Why is a thread-safety fix needed in borrow?
2. Which change request adds an interface and one class per option?
3. What problem has a hold that is never used?
Words you will meet
| Word | Meaning |
|---|---|
| Title | A book as information, shared by all its copies |
| Copy | One physical item that can be lent |
| Loan | A record of a copy lent to a member |
| Hold | A place in the queue for a title with no free copy |
| Renewal | An extension of a loan's due date |