Topic 043 / Core
Object-oriented design
Chapter 4.1 · Object-Oriented Design and SOLID
What and why
As a program grows past a handful of functions, raw functions and global data drift apart: state and the behavior that keeps it valid live in different places, and any caller can mutate state in any order. Object-oriented design (OOD) groups state with the behavior that protects it, hides the internal representation behind a small interface, and expresses variation as alternative implementations of that interface instead of branching on type. Precisely: OOD structures a program as objects — state plus the behavior that owns it — related by composition (has-a, assembled at runtime) and polymorphism (a shared interface with interchangeable implementations), preferring composition over inheritance because inheritance couples a subtype to its parent's implementation, not only its contract. Builds on: → 1 (programming fundamentals — functions, composite data, state).
Diagram
Inheritance (is-a, fixed at compile time) Composition (has-a, assembled at runtime)
───────────────────────────────────────── ──────────────────────────────────────────
class EmailNotifier { send(); format(); } class Notifier {
class SmsNotifier extends EmailNotifier { constructor(channel: Sender) {}
override send(); notify() { channel.send(...) }
// inherits format() too — even if wrong }
// for SMS, with no way to opt out // swap `channel` for any Sender —
} // Notifier never changes
How it works
- Identify the nouns that carry state across calls (Task, Project, Comment) — these are candidate objects; identify the verbs attached to each (assign, close, comment) — these become methods.
- Encapsulate: make fields private and expose only the operations that keep state valid.
A caller cannot set a Task's status directly; it calls
task.complete(), which checks that the transition is legal (→ 46). - Prefer composition: a Task has an Assignee and a Status; it does not inherit from a generic base to get them. Reuse behavior by injecting a collaborator, not by extending a class that happens to have it.
- Use polymorphism through a shared interface only when behavior genuinely varies by type (different reminder channels) — callers depend on the interface, never on which concrete type implements it.
- Keep objects small. An object modeling two unrelated responsibilities is the first sign that Single Responsibility (→ 44) will need to apply.
Practical example
(illustrative). In the task manager: a Task holds id, title,
status, and an Assignee value, and exposes assign(user) and complete() — never a
raw setStatus(value). complete() itself checks that the current status allows
completion before changing it, so the invariant lives in exactly one place instead of in
every caller that touches a Task.
When to use it
- Use when: multiple related variants of behavior exist or are a realistic near-term need, or state has invariants worth protecting from arbitrary mutation.
- Avoid when: a plain function with a few parameters already does the job — wrapping one operation in a class with one method is ceremony.
- Failure scenarios: anemic objects (all fields public, all logic in a separate "service" class — the object buys nothing); deep inheritance hierarchies that force the whole tree to change together for one subtype's need.
- Simpler alternative: a plain function and a record/struct. Promote to a full object only once behavior and invariants accumulate around the data.
Practice
(15–20 min; no lab services — any language). Take an inheritance-based
pair — EmailNotifier and SmsNotifier extends EmailNotifier, where SmsNotifier
overrides send() but inherits a format() that produces the wrong output for SMS —
and refactor to composition: a Notifier that takes an injected Sender interface.
Done when adding a third channel requires adding one new Sender implementation and zero
changes to Notifier.
Key takeaway
Objects bundle state with the behavior that protects it; compose by default, inherit only for genuine is-a substitutability. Done when you can: point to one of your classes and name the exact invariant its encapsulation protects. Used next in → 44 (SOLID turns these instincts into checkable rules), → 47 (patterns compose and vary objects systematically).
References
None — this topic makes no external claims.
Source directory