Handbooks / OOP & Design Patterns / Chapter 4
SOLID Principles
7 pages · ~5 min✓ Reviewed
Builds on Interfaces vs Abstract Classes. Next up: Coupling and Cohesion.
SOLID Mental Model
1. What SOLID Is
- SOLID = five class-design rules for easy-to-change code
- SRP, OCP, LSP, ISP, DIP
- Core goal: one requirement change touches one place
- Not a ripple across many classes
2. Smell to Principle
| Smell | Principle |
|---|---|
| Giant class | SRP |
| Growing if/else on type | OCP |
| Subclass that throws | LSP |
| Unused methods | ISP |
new of concrete deps | DIP |
3. Judgment
- Guidelines for managing change, not laws
- Apply when a real change pressure appears
Remember
No change pressure, no refactor. Do not pre-split for imagined futures.
4. Interview Point
Single Responsibility and Open/Closed
1. Single Responsibility
- One reason to change = one actor owns the class
- Bad:
InvoicehascalculateTotal(),saveToDb(),printPdf() - Tax, DB and layout changes all edit one class
| Class | Owns | Changes with |
|---|---|---|
Invoice | data, totals | tax rules |
InvoiceRepository | persistence | DB |
Liskov Substitution Principle
1. The Rule
- LSP: a subtype works wherever its base type is expected
- No stronger preconditions (what callers must supply)
- No weaker postconditions (what callers get back)
- Keep every base invariant
2. Violations
Squaresetters change both sides, so aRectanglecaller gets the wrong areaPenguin.fly()throws: any caller holding a Penguin as a Bird crashes
python
r = Square() # extends Rect r.set_width(5) r.set_height(4) r.area() # caller expects 20, gets 16
Interface Segregation and Dependency Inversion
1. Interface Segregation
- ISP: no client forced to depend on methods it does not use
- Prefer many small role interfaces
- Violation:
Workerhaswork()andeat() Robotmust no-op or throw ineat()- Fix: split into
WorkableandEatable
2. Dependency Inversion
- DIP: high-level modules depend on abstractions
- Both high and low level depend on the interface
- Violation:
OrderServicerunsself.db = MySQLDatabase()
Common SOLID Mistakes
1. Over-Applying SOLID
- Over-splitting: one-method classes hide the flow; split only on a real second reason to change
- One interface per class, one implementation, no test seam: noise, not DIP
- Type switch in one place, no new types expected: leave it (rule of three)
2. Inheritance vs Composition
| Inherit for reuse | Compose | |
|---|---|---|
| Link | is-a by convenience | has-a, delegates |
| LSP |
SOLID Cheat Sheet
1. Smell and Fix
| Rule | Smell | Fix |
|---|---|---|
| SRP | DB + logic + output in one class | Split responsibilities |
| OCP | if/elif on type | Polymorphism or Strategy |
| LSP | Override throws or changes semantics | Composition, smaller interfaces |
| ISP | Empty or throwing methods | Split the interface |
| DIP | new Concrete() in a service |
Check yourself
A Square extends Rectangle. Its setWidth also sets height, and setHeight also sets width. After r.setWidth(5); r.setHeight(4) on a Square, what is r.area()?
- 16: the second call resets both sides to 4
- The caller expects 20, so Square is not substitutable: an LSP break
Penguin extends Bird and its fly() throws. A loop calls fly() on every Bird. What breaks, and what is the fix?
- The loop crashes on the Penguin: an LSP break
- Move
fly()into aFlyerinterface that only flyers implement