Inversion of control: replace hand-wired flow with hooks
Write a rule for every batch once, and let each job fill in its part.
Watch the narrated video
Every step
Problem
Steps 1–4Refactor
Steps 5–11Payoff
Steps 12–13Recap
Steps 14–15The code: before and after
Before
class ShipmentJob:
def run(self, shipment_ids: list[int]) -> None:
print("ship: start")
for item_id in shipment_ids:
print(f"label {item_id}")
print(f"ship: done, items: {len(shipment_ids)}")
class RefundJob:
def run(self, refund_ids: list[int]) -> None:
print("refund: start")
for item_id in refund_ids:
print(f"refund {item_id}")
print(f"refund: done, items: {len(refund_ids)}")
if __name__ == "__main__":
ShipmentJob().run([101, 102])
RefundJob().run([7])After
from typing import Protocol
class BatchStep(Protocol):
def handle(self, item_id: int) -> None: ...
class BatchRunner:
def run(self, name: str, ids: list[int],
step: BatchStep) -> None:
print(f"{name}: start")
for item_id in ids:
step.handle(item_id)
print(f"audit {name} {item_id}")
print(f"{name}: done, items: {len(ids)}")
class ShipmentJob:
def handle(self, item_id: int) -> None:
print(f"label {item_id}")
class RefundJob:
def handle(self, item_id: int) -> None:
print(f"refund {item_id}")
class RestockJob:
def handle(self, item_id: int) -> None:
print(f"restock {item_id}")
if __name__ == "__main__":
runner = BatchRunner()
runner.run("ship", [101, 102], ShipmentJob())
runner.run("refund", [7], RefundJob())
runner.run("restock", [55], RestockJob())Transcript, step by step
Problem
Step 1: Two jobs, two loops Before
These are a warehouse's night batches, and Shipment Job prints a start line, one label per item, and a done line. Refund Job does the same with its own words, a start line, one refund line per item, and a done line. The main block ships items 101 and 102. Then it refunds item 7, so the night prints seven lines. Each job was small and written on a different day, so writing the loop out in each one was the shortest path.
Step 2: An audit line per item Change request
Now compliance asks that every batch write one audit line per item, right after the item is handled. The quick way adds an audit print inside Shipment Job's loop, and another one inside Refund Job's loop. Mypy passes, and the output gains audit ship 101, audit ship 102, and audit refund 7.
Step 3: Every job wires the flow Code smell
The fixed order of a batch, a start line, the work for each item, and a done line, is its flow. Here each job wires that flow by hand, so a rule for every batch has to be written into every job. You can see it in the shipment loop here, its audit line here, the refund loop here, and its audit line here. The same audit rule now exists twice, and each new job means pasting it into one more loop. If one copy forgets the audit line, nothing stops it, and that batch runs silently unaudited.
Step 4: Flip who runs the loop Principle
You might extract an audit helper function, but each loop would still have to call it. Forget that call in one job, and that batch still runs unaudited. So the loop should live in one place, and each job should only say what happens to one item. The fix flips who runs the loop, so one runner runs it and calls each job instead.
Refactor
Step 5: Refactor first Back it out
So we take both audit lines back out, and the jobs print their seven lines again. We refactor in small steps with the same output each time, and the audit comes back at the end.
Step 6: Shipment's per-item work Extract Method
In Shipment Job, we extract the label print into its own method, handle, which takes one item id. The loop now calls handle for each id, and the output is the same.
Step 7: Refund's per-item work Extract Method
The same move in Refund Job, so its refund print becomes a handle method too. Its loop calls handle for each id, and both jobs now keep their per-item work in one method.
Step 8: Name the hook Extract Interface
Both handle methods have the same shape, so we extract an interface, a Protocol named Batch Step, with that one method. A Protocol checks only the shape, so Shipment Job and Refund Job fit it without inheriting anything.
Step 9: A runner owns the flow Extract Class
Now we extract a class, Batch Runner, and move the flow into its run method. It takes a batch name, the ids and a Batch Step, then prints the start line, loops, and prints the done line. Inside the loop, it calls the step's handle for each id. A method the runner calls at a fixed point in its flow is a hook. Shipment Job's run now hands its name, its ids and itself to the runner, and mypy checks it fits Batch Step. So the runner now calls Shipment Job, not the other way round, and that flip is inversion of control. The output still matches the first version, line for line.
Step 10: Refund uses the runner Replace Inline Code with Function Call
Refund Job's run still wrote the same flow by hand, so we replace that code with a call to the runner. It passes refund, its ids and itself, and the output stays the same.
Step 11: Each job is only its hook Inline Method
Both run methods only forwarded to the runner, so we inline them into the main block and delete them. The main block builds one Batch Runner and runs ship and refund, each with its ids and a new job. Each job is now only its hook, one handle method that says what happens to one item.
Payoff
Step 12: The audit, take two Payoff
So the audit request from the start can come back in now. It's one line in Batch Runner's loop, right after the step handles the item. The output is the same as the quick version, with audit ship 101, audit ship 102, and audit refund 7. Shipment Job and Refund Job didn't change at all. Where the shipment loop and its audit line stood, there's only a handle method now, and the same goes for refund. The audit rule exists once, in the one loop that every job runs through.
Step 13: A third job Payoff
But the real test is a new job, and Restock Job has only a handle method that prints restock and the id. The main block runs it with item 55 under the name restock. Restock Job has no loop and no audit line, so what does its batch print? Restock 55, then audit restock 55, because the runner's loop writes that line. The runner owns the flow and calls each job's hook, so a rule for every batch is one edit in one place. But every job must now fit the runner's one flow, and a job with a different order can't use it. You also read two classes to follow one batch.
Recap
Step 14: Where we started, where we ended Before / after
On the left, each job ran its own flow, with its own start line, loop, and done line. So a rule for every batch, like the audit line, had to be pasted into each loop. On the right, Batch Runner owns the flow and calls each job's hook, with the audit written once. And each job only fills in what happens to one item, in one handle method.
Step 15: When to reach for it Takeaways
Reach for it when several jobs share one fixed flow and differ only in the work they do per item. Skip it when there's one job, or when the flows really run in a different order. A runner then only adds a second place to read, with nothing shared for it to own. Spot it by the same loop, with the same start and done lines, copied into several classes. And Python's unittest works this way, because its runner calls your set up method right before each test.