|
1 | 1 | """The mediator without a Colleague hierarchy. |
2 | 2 |
|
3 | | -Widgets take a ``notify`` callable; the coordinator holds every interaction |
4 | | -rule in one place and the widgets hold none. |
| 3 | +A checkout form with enough interdependent rules to *justify* a mediator: |
| 4 | +country restricts shipping methods, shipping method gates payment options |
| 5 | +and recomputes the total, and submit is enabled only when the whole set is |
| 6 | +coherent. Widgets know none of it -- every rule lives in one method. |
5 | 7 | """ |
6 | 8 |
|
7 | 9 | from __future__ import annotations |
8 | 10 |
|
9 | 11 | from collections.abc import Callable |
| 12 | +from dataclasses import dataclass, field |
10 | 13 |
|
| 14 | +SHIPPING_BY_COUNTRY = { |
| 15 | + "CA": {"standard": 900, "express": 2400}, |
| 16 | + "US": {"standard": 700, "express": 1900}, |
| 17 | + "DE": {"standard": 1100}, # no express lane |
| 18 | +} |
| 19 | +#: cash-on-delivery is only offered on express shipments |
| 20 | +PAYMENTS_BY_SHIPPING: dict[str, tuple[str, ...]] = { |
| 21 | + "standard": ("card",), |
| 22 | + "express": ("card", "cod"), |
| 23 | +} |
11 | 24 |
|
12 | | -class TextField: |
13 | | - def __init__(self, notify: Callable[[], None]) -> None: |
14 | | - self.text = "" |
15 | | - self._notify = notify |
16 | 25 |
|
17 | | - def type_text(self, text: str) -> None: |
18 | | - self.text = text |
19 | | - self._notify() |
| 26 | +@dataclass |
| 27 | +class Field: |
| 28 | + """A dumb widget: holds a value, reports changes. No rules.""" |
20 | 29 |
|
| 30 | + notify: Callable[[], None] |
| 31 | + value: str = "" |
21 | 32 |
|
22 | | -class SignupForm: |
23 | | - """The mediator: rules in one readable method.""" |
| 33 | + def set(self, value: str) -> None: |
| 34 | + self.value = value |
| 35 | + self.notify() |
24 | 36 |
|
25 | | - def __init__(self) -> None: |
26 | | - self.username = TextField(self._recheck) |
27 | | - self.password = TextField(self._recheck) |
28 | | - self.submit_enabled = False |
| 37 | + |
| 38 | +@dataclass |
| 39 | +class CheckoutForm: |
| 40 | + """The mediator: every cross-field rule, in one readable place.""" |
| 41 | + |
| 42 | + cart_cents: int |
| 43 | + country: Field = field(init=False) |
| 44 | + shipping: Field = field(init=False) |
| 45 | + payment: Field = field(init=False) |
| 46 | + shipping_options: tuple[str, ...] = () |
| 47 | + payment_options: tuple[str, ...] = () |
| 48 | + total_cents: int = 0 |
| 49 | + submit_enabled: bool = False |
| 50 | + |
| 51 | + def __post_init__(self) -> None: |
| 52 | + self.country = Field(self._recheck) |
| 53 | + self.shipping = Field(self._recheck) |
| 54 | + self.payment = Field(self._recheck) |
| 55 | + self._recheck() |
29 | 56 |
|
30 | 57 | def _recheck(self) -> None: |
31 | | - self.submit_enabled = bool(self.username.text) and len(self.password.text) >= 8 |
| 58 | + lanes = SHIPPING_BY_COUNTRY.get(self.country.value, {}) |
| 59 | + self.shipping_options = tuple(lanes) |
| 60 | + if self.shipping.value not in lanes: |
| 61 | + self.shipping.value = "" # country change invalidated the lane |
| 62 | + self.payment_options = PAYMENTS_BY_SHIPPING.get(self.shipping.value, ()) |
| 63 | + if self.payment.value not in self.payment_options: |
| 64 | + self.payment.value = "" |
| 65 | + self.total_cents = self.cart_cents + lanes.get(self.shipping.value, 0) |
| 66 | + self.submit_enabled = bool( |
| 67 | + self.country.value and self.shipping.value and self.payment.value |
| 68 | + ) |
32 | 69 |
|
33 | 70 |
|
34 | 71 | def main() -> None: |
35 | | - form = SignupForm() |
36 | | - form.username.type_text("ada") |
37 | | - form.password.type_text("short") |
38 | | - print(f"weak password: {form.submit_enabled}") |
39 | | - form.password.type_text("correcthorse") |
40 | | - print(f"valid form: {form.submit_enabled}") |
| 72 | + form = CheckoutForm(cart_cents=5000) |
| 73 | + form.country.set("CA") |
| 74 | + form.shipping.set("express") |
| 75 | + form.payment.set("cod") |
| 76 | + print(f"total {form.total_cents}, submit={form.submit_enabled}") |
| 77 | + form.country.set("DE") # express vanishes; dependent fields reset |
| 78 | + print(f"after DE: shipping={form.shipping.value!r}, submit={form.submit_enabled}") |
41 | 79 |
|
42 | 80 |
|
43 | 81 | if __name__ == "__main__": |
|
0 commit comments