Skip to content
Ameen Abozaid
In reviewJNR-004-L02

Open/Closed

  • Lesson 2 of 7
  • 11 min read
  • Junior
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/else or switch on 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

java
interface TaxRule { Money tax(Money net); }
class VatRule implements TaxRule { ... }
// adding a new rule = adding a class

Check 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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.