Skip to main content
InterviewHubPRO
Section Overview
Q37

Question 37 of 50

Part 3: Enterprise Architecture, Package Design & Enforcement (25 - 35)

74%
SOLID & OOP Principles (50 Qs)•Part 3: Enterprise Architecture, Package Design & Enforcement (25 - 35)

Open/Closed Principle (OCP) via Strategy and Plugin Architectures.

senior LevelCode ExampleSystem Design

In Simple Words: "Software entities should be open for extension, but closed for modification":

  • When adding a new shipping carrier (DHL) to an e-commerce platform, you should never open and edit existing shipping calculation code with a new switch(carrier) case DHL: statement.
  • Plugin / Strategy Architecture:
    typescript
    interface ShippingStrategy {
      calculateCost(order: Order): Money;
    }
    class DHLShippingStrategy implements ShippingStrategy {
      calculateCost(order: Order): Money { ... }
    }
    
  • You simply add a new class implementing the interface and register it in the dependency container. Existing tested code remains 100% untouched and closed to edits.