Skip to the guide

Prices, fees and rules that keep changing

IntermediatePattern7 pages

Prices change more often than anything else in a design, and when several rules apply, the order they run in can change the total. This move gives each rule its own class (Strategy), and it fixes the order in one place by putting the rules in stages. Adding a rule then changes no old rule (Open/closed). Interviewers listen for what happens when a cap and a discount both apply, and for when a plain sum is enough.

A tax form

Think of a yearly tax form. Part A lists your income, line by line, and you can fill those lines in any order because they only add up. Part B lists allowances, and each is worked out from the total of Part A. So these lines can go in any order too. The last line says "the lesser of this figure or the maximum". A new allowance is one more line in Part B, and the parts stay in the same order.

In code, the form is this page's Tariff, the full set of price rules for a bike rental. Part A is a list of Charge classes, and Part B is a list of Discount classes. Part A's total is the subtotal that every discount sees, and the last line is the cap.