Design an inventory management system
medium 80 minCount goods in several places, promise them to orders without ever promising too much, ship or let go, and explain every count.
Watch lesson4:28
Patterns
Used, refused, and why
The point: two patterns solve a problem that is really here. The others are named so that the interviewer hears why they are not.
Used
Patterns that earn their place
| Pattern | Where | What it buys |
|---|---|---|
| Strategy | PickPolicy | Near first, cheapest, fewest parcels, without touching reserve |
| Observer, in its smallest form | LowStock | The inventory does not know mail, chat or buying |
| Record of events | Movement | Counts can be explained and rebuilt |
| Dependency injection | Policy, clock, listener | A test owns time |
Refused
Patterns people add by habit
| Pattern | Why not |
|---|---|
| Singleton | One inventory at run time is one new in main. Two in a test must be possible |
| State | Three verbs, one open state. An enum and one check are shorter and complete |
| Factory | Nothing to choose: one kind of line, one kind of promise |
| Command | A movement looks like one, but it is the past and cannot be executed |
| Inheritance of products | No behaviour differs |
Principles
SOLID, with one proof each
| Letter | Proof in this code |
|---|---|
| S | A change to the choice of place opens one file. A change to alerts opens another |
| O | InOrder was added without editing Inventory |
| L | Every policy answers with a full plan or None. One that gave a part would break reserve |
| I | LowStock has one method. A mailer does not have to implement ten |
| D | Inventory gets a clock and a policy. It creates neither |
One policy for the place. What if rules are combined?
BreaksFlags
- How
plan(qty, lines, near=True, cold=False)- Limit
- Every new rule edits every caller.
- Choose when
- Never.
WorksSubclasses for combinations
- How
NearAndColdFewest- Limit
- Two rules with three values each are nine classes.
- Choose when
- Two combinations that never grow.
StrongFilter, then order
- How
- A filter removes places that may not send. An order sorts the rest.
filltakes from the front. - Limit
- Two small interfaces in place of one.
- Choose when
- As soon as a rule says must and another says rather.