AlgoViz

Design an inventory management system

medium 80 min

Count goods in several places, promise them to orders without ever promising too much, ship or let go, and explain every count.

Watch lesson4:28

UML design

The classes and their arrows

The point: all arrows point away from the coordinator, and only one class can change a count.

The design to reach: one coordinator, one guarded line, plain records

Inventory- lines: Map<sku+place, StockLine>- reservations: Map<id, ...>- movements: List<Movement>- lock: Lock+ receive(sku, place, qty, ref)+ reserve(order, wanted, seconds)+ ship(id) / release(id)+ expireDue(): int

1/7Start from the verbs

Goods arrive, are promised, leave, or the promise ends. Four verbs on one coordinator, Inventory. Callers see nothing else.

implementsusesowns (dies together)

A question mark means that the answer may be nothing. Not enough stock is an everyday answer for a policy.

Read it

Arrows that people mix up

From, toRelationWhy this one
Inventory, StockLineOwnsNobody else has a reference. A line outside the lock could be changed unseen
Inventory, PickPolicyUsesHanded in. It may be shared, and it has no state worth guarding
Reservation, PickOwnsA pick means nothing without its promise
Pick, StockLineNo arrowIt holds the names and not the line. A pick in a log must not let anyone change stock
Policy, StockLineReads a listIt receives lines for one call and answers with picks. C++ can say so with const

The same

As a text diagram for the whiteboard

Six boxes fit on a whiteboard in three minutes. Records can be named without fields.

Should the reservation give itself back?

Breaksreservation.cancel() changes the lines
How
The promise has references to its lines and calls them.
Limit
The lines change outside the inventory lock, and the caller can cancel twice from two threads.
Choose when
Never, with threads.
WorksThe reservation calls the inventory
How
reservation.cancel() calls inventory.release(self.id).
Limit
Two objects that know each other, for a shorter line at the caller.
Choose when
When callers hold reservations for a long time and like methods on them.
StrongThe inventory ends promises by id
How
inventory.release(id)
Limit
Callers must keep a number.
Choose when
The default: one door, one lock, and an id survives a restart.