Object-Oriented Design — Junior¶
An object combines state with behavior that protects valid state. Avoid classes that are only bags of fields while services manipulate them from outside.
sequenceDiagram
Checkout->>Order: addItem(product, quantity)
Order->>Order: validate status and quantity
Order->>Order: update lines and total
Order-->>Checkout: accepted or domain error
Use small names from the domain. Keep constructors valid. Hide mutation that could violate invariants. Prefer composition for “has-a” relationships; use inheritance only for a genuine substitutable “is-a” contract.
KISS and YAGNI mean implement the simplest design that expresses today’s verified behavior. DRY applies to knowledge, not every similar line.
Test yourself¶
- Which object should protect an order’s status rule?
- Why is a public setter risky?
- When does composition beat inheritance?
- What knowledge is truly duplicated?
Continue to middle.md.