Practice
WeekendFeeStrategy to the Restaurant Management System's billing module to apply extra charges on weekends. Given the current design uses a FeeStrategy interface for fees, what is the minimum change required to add this feature while respecting SOLID principles?Solution
Step 1: Identify the SOLID principle involved
Open-Closed Principle (OCP) requires adding new behavior without modifying existing code.Step 2: Evaluate options for minimal change and OCP compliance
Create a new WeekendFeeStrategy class implementing FeeStrategy and inject it where needed without modifying existing classes. respects OCP by adding a new strategy class without changing existing ones. Add a boolean weekend flag to the Bill class and modify its calculateTotal() method to add fees. modifies Bill class, violating OCP. Modify the existing FeeStrategy implementation to check for weekends and apply extra fees. modifies existing FeeStrategy, violating OCP. Add weekend logic inside the Bill class directly, bypassing FeeStrategy. adds logic in Bill, violating SRP and OCP.Final Answer:
Option A -> Option AQuick Check:
New FeeStrategy class -> OCP respected [check]
WinStrategy interface for win detection. To add a WeekendWinBonusStrategy that applies only on weekends, what is the minimum change required to comply with SOLID principles?Solution
Step 1: Understand OCP and DIP in current design
Open-Closed Principle (OCP) requires adding new behavior without modifying existing classes. Dependency Inversion Principle (DIP) encourages depending on abstractions.Step 2: Evaluate options for minimal change
Create a newWeekendWinBonusStrategyclass implementingWinStrategyand use it without modifying existing classes. adds a new strategy class but requires changing client code to use it, violating minimal change. Add a boolean flag in theBoardclass to toggle weekend mode and update all win checks accordingly. adds a toggle flag in Board, allowing existing strategies to adapt behavior without modifying their code, preserving OCP better than A or D.Final Answer:
Option B -> Option BQuick Check:
Toggle flag in Board -> minimal change, respects OCP [check]
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot should not require modifying existing classes to avoid breaking OCP.Step 2: Evaluate options
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility.COMPATIBILITY_MAP accordingly. modifies enums and compatibility map, changing existing code; Modify Vehicle and Spot classes to add boolean isElectric and isChargingSpot flags, then update existing handlers to check these flags. adds flags and updates handlers without changing existing class hierarchies; Create a new ElectricCarHandler in the chain that handles electric cars and charging spots, modifying the chain setup to include it. modifies chain setup, which is acceptable but may require modifying chain initialization; Add a new method in Vehicle interface for charging compatibility and implement it in ElectricCar, modifying existing handlers to call it. modifies Vehicle interface, breaking existing implementations.Step 3: Choose minimal invasive approach
Modify Vehicle and Spot classes to add boolean isElectric and isChargingSpot flags, then update existing handlers to check these flags. adds minimal changes with flags and updates handlers, avoiding class explosion or interface changes.Final Answer:
Option B -> Option BQuick Check:
Flags added, existing classes unchanged, handlers updated [check]
ElectricCar jobs requiring ChargingSpot resources in the Job Scheduler, what is the minimum change to the current design to maintain extensibility without modifying existing classes?Solution
Step 1: Identify extensibility requirements
ElectricCar and ChargingSpot are new types requiring minimal impact on existing Vehicle and Spot classes.Step 2: Apply extensibility without modifying existing classes
Add new enum values for types, create ElectricCar subclass, and update SpotCompatibility map externally to maintain OCP and avoid class explosion.Final Answer:
Option B -> Option BQuick Check:
New enums + subclass + external compatibility update -> minimal existing code change [check]
ElectricCar vehicles requiring ChargingSpot compatibility. What is the minimum change to the current design to add this without modifying existing classes?Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot must not modify existing classes to respect OCP and avoid class explosion.Step 2: Use enums and subclassing
Adding new enum values and a new ElectricCar subclass extends functionality cleanly; updating compatibility map centralizes spot-vehicle matching without changing existing code.Step 3: Avoid modifying base classes or Executor
Modifying Vehicle or Executor violates OCP and risks introducing bugs.Final Answer:
Option D -> Option DQuick Check:
New enums + subclass + compatibility map update -> minimal, no existing class change [check]
