Modelling and persistence
What are the SOLID principles?
On this page
Single responsibility, open–closed, Liskov substitution, interface segregation and dependency inversion are design heuristics. Apply one to a concrete pressure. If changing a PDF header forces a tax rules test to change, separate rendering from calculation and check that each now changes independently.
Worked example
Pythonfrom typing import Protocol
class TaxPolicy(Protocol):
def cents(self, subtotal: int) -> int: ...
class Exempt:
def cents(self, subtotal: int) -> int:
return 0
def invoice_total(subtotal: int, tax: TaxPolicy) -> int:
return subtotal + tax.cents(subtotal)
assert invoice_total(500, Exempt()) == 500Explore class relationships in the OOP Playground