Design Principles & SOLID
Master SOLID (SRP, OCP, LSP, ISP, DIP) plus DRY, KISS, YAGNI and composition over inheritance — each principle with a bad-vs-good example, a runnable Kotlin demo, a graded exercise, and a class-diagram animation in the Visualize tab.
Explain the decision and prove the behavior
Answer before opening the guide. Then solve the exercise, test an edge case and explain when you would not introduce the abstraction.
SRP: reasons to change
A service validates, saves and emails an order. Which changes belong together?
Practice in Kotlin →Answer guide
Group behavior by the stakeholder or policy that changes it. Extract persistence and notification boundaries when they vary independently. SRP does not mean one method per class.
OCP: a useful extension point
Would you add an interface or a sealed when for a new pricing rule?
Practice in Kotlin →Answer guide
A function parameter can be enough for one varying calculation. Prefer an interface for independently extensible behavior; use a sealed hierarchy when the variants are deliberately closed and exhaustive handling is valuable.
LSP: preserve the contract
A subtype throws for a method promised by its interface. Does compiling prove substitutability?
Practice in Kotlin →Answer guide
No. Verify behavior: accepted inputs, postconditions and invariants. Do not strengthen preconditions or weaken promised results. Write the same contract tests for every implementation; do not require all implementations to have identical internal code.
ISP: contracts shaped by clients
Should a basic printer implement scan() and throw UnsupportedOperationException?
Practice in Kotlin →Answer guide
Separate printing and scanning capabilities. The print client needs only the printing contract. Kotlin interface delegation can compose capabilities, but splitting interfaces is justified by client needs, not by an arbitrary method-count rule.
DIP: testable policy boundaries
How would you test order placement without a database or email service?
Practice in Kotlin →Answer guide
Inject small contracts through the constructor and use in-memory fakes. Dependency injection is a wiring technique; DIP is the dependency direction. Assert outputs and essential side effects, and separately integration-test the real adapters.
Patterns and code review · 45-minute mock
5 min: identify the requested change and observable behavior. 20 min: refactor with Strategy, Decorator or an injected function; justify the choice. 10 min: test boundaries, effects and failures. 10 min: review naming, null-safety, mutability, coupling and complexity. Avoid !! without a contract, do not silently swallow exceptions, and consider ownership/cancellation when coroutines are involved.
Score each criterion 0–2: correct behavior, tests that detect the defect, explained trade-off, Kotlin clarity and error handling. Passing stdin/stdout tests alone does not establish design quality; review the structure too.
Patterns → · Refactoring →- Step 1
Clean design principles: an overview
Names for the forces that keep code soft
Start lesson - Step 2
Single Responsibility Principle
One class, one reason to change
Start lesson - Step 3
Open/Closed Principle
Open for extension, closed for modification
Start lesson - Step 4
Liskov Substitution Principle
A subtype must work anywhere its base type is expected
Start lesson - Step 5
Interface Segregation Principle
Many small roles beat one fat interface
Start lesson - Step 6
Dependency Inversion Principle
Depend on abstractions, not concretions
Start lesson - Step 7
DRY — Don't Repeat Yourself
Every fact lives in exactly one place
Start lesson - Step 8
KISS & YAGNI
Build for today, not for an imagined tomorrow
Start lesson - Step 9
Composition over Inheritance
Build objects from swappable parts (has-a), not deep class trees (is-a)
Start lesson - Step 10
Capstone: refactor a God-class to SOLID
One class, four jobs — split it, inject it, extend it
Start lesson