Behavioral Patterns: Worked Examples
1. A policy interface does not establish units
For 105 cents, 10% reduction with half-up rounding gives 95 cents: (105×90+50)//100. An implementation returning floating-point 0.945 dollars has changed both type and unit. A fake that returns 95 can test the caller's boundary; it cannot validate the real policy arithmetic. Keep both kinds of evidence.
2. Snapshot membership with two registrations
Register the same callback as tokens 0 and 1. Dispose token 0 twice. Only token 1 remains. Now A removes B during delivery: B is in this call's snapshot and runs once, but is absent from later calls. Snapshotting membership does not freeze a mutable event object or make delivery transactional.
3. Invalid edits preserve future history
| Step | Text | Undo available | Redo available |
|---|---|---|---|
| Start | a | no | no |
| Append b | ab | yes | no |
| Undo | a | no | yes |
| Invalid replacement | a | no | yes |
| Redo | ab | yes | no |
Clearing redo before validating the replacement fails this trace. Saving only a command object whose backup is overwritten can fail a different repeated-use trace.
4. An attempted command is not an accepted fact
Archive while draft rejects; accepted records remain empty. Submit then approve produce two contiguous facts and final state published. Replaying those facts does not call observers. A failed observer after approval must not turn the accepted transition into a second approval on retry.
5. Laziness is observable
Page 0 contains 10,20 and indicates more. Construction fetches nothing. Two next calls fetch only page 0. A third call is needed before page 1 is requested. A prefetch implementation can return the same values and still violate this contract.
6. Similar structure, different policy
Authentication success returns allow in a first-match engine, so later checks do not run. In a pipeline requiring all checks, authentication success must continue. Renaming both structures Chain of Responsibility cannot reconcile that difference.
Predict changed cases before consulting practice feedback: valid no-op edit after undo; empty nonfinal page; duplicate subscription; acknowledgement loss after an external effect. Explain forbidden calls as well as results.