How do design patterns help in adhering to OCP?
Short answer: Several design patterns are built around the idea of making systems extensible without modifying core logic: Pattern How It Helps With OCP Strategy Allows changing behavior by swapping strategies.
Explain a bit more
Decorator Adds new responsibilities dynamically without changing original code. Template Method Allows subclasses to override certain steps in an algorithm. Factory Method Makes it easy to introduce new types without altering existing logic. Observer Extends behavior in reaction to events without altering the source.
Real-world example (ShopNest)
Open/Closed in ShopNest: add a new payment method by adding a class, not by editing a giant switch in CheckoutService.
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png