Open/Closed
On This Lesson 4 topics
The content for this lesson is written and waiting for review. The outline below shows what it covers. It will be published once it is approved.
In this lesson
add behavior without editing working code.
Planned topics
- O: open for extension, closed for modification.
- Smell: an
if/elseorswitchon type that grows with every new feature. - Fix: an interface with a new class per case.
- Then a new case means a new class, and old classes stay untouched.
- With a sealed type, the compiler lists what to update; both styles can be fine.
Draft outline details
Demonstration
interface TaxRule { Money tax(Money net); }
class VatRule implements TaxRule { ... }
// adding a new rule = adding a classCheck your understanding
What does "closed for modification" mean?
Show answer
Adding a feature does not require editing existing, tested code.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.