Lesson 2: OCP with Strategy
Every new store promotion adds another branch to the pricing method: a VIP coupon, a holiday sale, and next month — one more. Each edit reopens code that already worked in production, endangers existing coupons, and forces retesting everything. This lesson teaches the Open/Closed Principle (OCP) and
OCP with Strategy is like a drawer of cookie cutters: want a new shape? Drop a new cutter into the drawer. Nobody takes the rolling pin apart for that.
- Strategy pattern
- Behaviors are swappable objects behind one interface; the engine runs the contract without knowing the implementations.
- Open/Closed Principle (OCP)
- Open for extension with new classes, closed for modification of code that already works and is tested.