04 — SOLID, GRASP & Design by Contract¶
Driving question: How do we turn popular design acronyms into precise reasoning?
Learning objectives¶
- Apply SRP in terms of reasons for change and responsibility allocation.
- Use OCP and polymorphism selectively, not as universal indirection.
- Reason about substitutability through behavioral contracts.
- Use GRASP responsibility patterns to explain object collaboration.
SOLID beyond slogans¶
SRP¶
A responsibility is better understood as a coherent reason for change than as “one method” or “one thing.”
OCP¶
Open–Closed Principle encourages extension without modification along predicted axes of variation. Building extension points everywhere is itself a design cost.
LSP¶
Subtype correctness is behavioral. If a client relies on a base-type contract, the subtype must preserve that contract. Signature compatibility is insufficient.
ISP¶
Interfaces should be shaped around client roles. Splitting interfaces mechanically can produce a large, hard-to-navigate type system.
DIP¶
Stable policy should not be controlled by volatile mechanism. Abstractions belong where the policy needs them, not automatically in a generic “interfaces” package.
GRASP¶
GRASP concepts—Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, Protected Variations—are useful because they focus on responsibility assignment.
Contracts¶
Contracts make expectations explicit through preconditions, postconditions, and invariants. They are particularly important when refactoring inheritance structures or automating transformations.
Design / research exercise¶
Find one common “SOLID violation” rule from a linter or blog. Construct a counterexample where following the rule mechanically makes the design worse. State the contextual condition the heuristic omitted.
Suggested reading¶
- Craig Larman, Applying UML and Patterns (GRASP).
- Bertrand Meyer, Object-Oriented Software Construction (Design by Contract).