Level 3: Object-Oriented Design
Design classes and interfaces so new requirements mean adding code, not editing fragile code. Level 3 of 11: 6 lessons on object-oriented design.
Outcome: Design classes and interfaces so new requirements mean adding code, not editing fragile code.
ShopFlow adds payments, discounts and shipping. The principles behind good object design — SOLID, composition, encapsulation — decide whether each new feature is a small addition or a risky edit.
The pressure on ShopFlow: Every new payment method or discount means editing the same huge classes.
Lessons in level 3
- Single Responsibility: One Reason to Change — A class should answer to one group of people with one kind of change.
- Open/Closed: Add Features Without Editing Old Code — Extend behaviour through new classes instead of growing if/else chains.
- Liskov Substitution: Subtypes That Keep Their Promises — When a subclass surprises its callers, inheritance becomes a trap.
- Interface Segregation & Dependency Inversion — Small interfaces owned by the caller, and business logic that does not depend on infrastructure.
- Composition Over Inheritance — Assemble behaviour from parts instead of building deep class trees.
- Encapsulation, Tell Don’t Ask & the Law of Demeter — Put behaviour next to the data it protects, and stop reaching through objects.
Source books
- Design Patterns — Eric Freeman, Elisabeth Robson, Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
- Pragmatic Programmer — David Thomas, Andrew Hunt