Holds: a line and a message
After this: Keep a line of members for a book, hold a returned copy for the first one, and tell them.
On this page · 6 sections · 3 checks
On day 2 Ben asks for Dune, but the only copy is with Ana. Cy asks for it too. When Ana brings it back, who should get it, and how do they find out? This lesson adds the last part of the paragraph: the line, and the message.
A line is a queue
Ben asked first, so Ben should get the copy first. A line where the first to join is the first to leave is a , from the lesson Queues: first in, first out in "Stacks and queues". The library keeps one queue of members for each book:
- Day 2: Ben reserves Dune. The line is Ben.
- Day 2: Cy reserves Dune. The line is Ben, then Cy.
- Day 20: Ana returns
DU-1. The library takes Ben off the front of the line. The line is Cy. - The copy is not put back on the shelf. Its status becomes on hold, and it remembers that it is held for Ben.
- The library tells Ben that Dune is ready.
Telling the member
How does the library tell Ben? It calls a method on him: update, with the message. In this program update prints the message; a real system might send an email instead. The library does not need to know how Ben is told. It only needs every member to have an update method. This is the pattern from "Three patterns you will meet first", in a small form. The line is the list of members who asked to hear about the book, and update is the one method the library calls. There is one difference: in that tutorial every subscriber is told about each upload, but here a returned copy can go to one member only, so only the first member in the line is told, and that member leaves the line.
The trace below runs a short version: a plain list stands in for the line. Taking the first item off a list is the slow way that the queues lesson warns about, but with two members it does not matter, and the whole program below uses a real queue. The task asks you to watch the fine, then the front of the line leaving.
- Run the programRun it and step through. First two members join the line, Ben in front of Cy. Then the fine for a copy due on day 15 and returned on day 20 is worked out. Then watch the first member leave the front of the line and get a message through the update method.
class Member: def __init__(self, name): self.name = name
def update(self, message): print("[to " + self.name + "] " + message)
def fine(due, day_back): late = day_back - due if late < 0: late = 0 return late * 5
holds = [Member("Ben"), Member("Cy")]print(fine(15, 20))if holds: first = holds.pop(0) first.update("Dune is ready for you")print(len(holds))run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
The return, as a picture
A sequence diagram shows the same return as calls between objects, top to bottom. Step through it.
Press Run it to send the messages one at a time.
- messages
- 0
The library asks the fine rule for the fine (25), holds the copy for Ben, calls Ben's update, and only then answers Ana with her fine.
Borrowing a held copy
One more change goes into borrow. A copy on hold is in the library, but only the member it is held for may take it. So borrow now accepts a copy that is either available, or on hold for the member who is asking. Everyone else is told there is no copy free.
The whole library
Here is the complete program, with the run from the paragraph. Python's line is a deque (a double-ended queue from collections): append adds at the back and popleft takes from the front. Java's line is an ArrayDeque: addLast adds at the back and pollFirst takes from the front. These do the same job as offer and poll in "Stacks and queues".
Read the new parts:
BookCopyhas a new field, held_forheldFor, empty until a copy is held.Memberhas the newupdatemethod.Librarykeepsholds: a queue for each ISBN, made empty when the book is added.reserveadds the member at the back and prints their place.- return_copyreturnCopy ends with a choice. If the book's line is not empty, it takes the first member off it, holds the copy for them and calls their
update. If the line is empty, the copy goes back on the shelf. borrowaccepts a copy that is free, or held for this member, and clears the hold when it lends it.
On day 21 Cy asks first and is turned away, because DU-1 is held for Ben. Ben then borrows it. The last line prints Ana's fines, Ana's loans and the length of Dune's line: 25 1 1. The one member still waiting is Cy.
Check yourself
On day 2 Ben joins the line for Dune, then Cy. On day 20 Ana returns DU-1. Who is in Dune's line straight after the return? The left end is the front.
On day 21 Cy asks to borrow Dune before Ben comes in. DU-1 is in the library. Why does Cy get “no copy of Dune free for Cy”?
These two lines are added at the very end of the whole run. Ben has DU-1, due on day 35, and Cy is first in Dune's line. What do they print?
lib.return_copy("DU-1", 30)lib.borrow(cy, "DU", 31)lib.returnCopy("DU-1", 30);lib.borrow(cy, "DU", 31);- Keep one queue of waiting members for each book, and take the first member off the front when a copy comes back.
- Hold a returned copy for that member, and let borrow accept a held copy only from the member it is held for.
- Tell the member through an update method every member has (Observer), so the library does not care how the message is sent.
- Read a sequence diagram of a return: the fine, the hold, the message, then the answer to the member who returned the book.