Skip to content
Commitwise
Menu

Open/closed principle

Also called: OCP

Code should be open for extension but closed for modification: add new behavior without editing what already works.

Definition

The "O" in SOLID. When a new variant (a payment method, a shipping option) arrives, you should be able to add it as new code instead of editing a growing switch statement in several places. Interfaces and patterns like Strategy are the usual way to get there.

In practice

Adding Apple Pay means writing an ApplePayPayment class that implements Payment, while Checkout stays untouched.

Learn it in context

Lessons that use this term.

Related terms

Esc

Type to search the whole site.