02 General-purpose structures: requirements first
General-purpose structures: requirements first
What a beginner should do here
The next sixteen problems are different ways to organize objects when requirements change. You do not need to guess a design pattern name before solving. Instead, read the caller example: What must be callable? What result and call order are required? What happens when a new object arrives?
For the first exercise, a calculator accepts a policy object. A policy is simply any object with a working apply(value) method. The caller supplies that object; your code calls it. A new policy can be passed later without editing your existing class. A second example: changing state is not the same as picking a policy. A Pending object may return Paid from transition("pay"); the owner saves that next state. This avoids checking if current == "pending" throughout the program. By contrast, a pricing strategy is an algorithm selected for the task and can stay unchanged while the calculation runs.
One Python word before the third challenge: a subclass written as class CsvJob(BaseJob): can override a method by defining the same name. A parent can keep the fixed workflow extract → transform → load while each subclass supplies those steps. The parent should stop before load if extract fails.
In every group the order is challenge → five SCOAT quiz questions → vocabulary lesson. The vocabulary lesson tells you the conventional pattern name after you reason about a working design. Compare your answer with its “nearby distinction” and ask whether you can simplify it.
Before opening hints, predict how the existing code should behave if the design-stress test introduces a new state, an unseen collaborator or another combination of roles. Don't add a class or interface unless the required behavior needs it.
There are sixteen exercises in the source-prescribed order. Each begins from concrete Expected Behavior. Reason through SCOAT, implement the smallest adequate design, and run the executable pressure first. Only the following vocabulary lesson names the traditional pattern that emerged.
Do not design backward from the expected pattern name. The design-stress test is the important evidence.
Loading sign-in options…