Practice
Match class has a private constructor and a static synchronized getInstance() method to ensure only one instance per match ID exists. Which design pattern is applied here and why is this the correct use case?Solution
Step 1: Identify the pattern from constructor and getInstance()
The private constructor prevents direct instantiation, and the static synchronized getInstance() ensures controlled, thread-safe instance creation.Step 2: Why Singleton fits this use case
Singleton ensures only one Match instance per match ID exists, avoiding inconsistent state across threads.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
WeekendFeeStrategy for premium commentary access pricing. Given the current design where FeeStrategy is an interface implemented by pricing classes, what is the minimum change required to add this feature following SOLID principles?Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires adding new behavior without modifying existing classes.Step 2: Evaluate options for OCP compliance
Adding a flag in the interface forces all implementations to change, violating OCP; creating a new class preserves OCP.Final Answer:
Option D -> Option DQuick Check:
New class implementing FeeStrategy -> OCP respected [check]
WeekendFeeStrategy to calculate parking fees differently on weekends using the current Iterator-based design. Which minimal change respects the Open-Closed Principle (OCP)?Solution
Step 1: Understand OCP requirements
OCP requires software entities to be open for extension but closed for modification.Step 2: Evaluate options
Modify the existing FeeStrategy class to add weekend logic with an if-else condition. modifies existing class, violating OCP. Create a new WeekendFeeStrategy class implementing FeeStrategy and update the client code to use it when appropriate. adds a new class but requires client code modification, which may violate DIP. Add a boolean flagisWeekendto the FeeStrategy interface and implement conditional logic in all subclasses. extends interface with a flag, allowing existing implementations to adapt without changing their core logic. Combine all fee strategies into a single class with multiple methods and switch logic inside the iterator. centralizes logic, violating SRP and OCP.Final Answer:
Option A -> Option AQuick Check:
Extending interface with flag allows extension without modifying existing classes [check]
Solution
Step 1: Identify extensibility requirements
New vehicle and spot types must be added without modifying existing classes to respect OCP.Step 2: Evaluate options
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map externally. adds new enum values and classes externally, updating compatibility map without changing existing code; Options B, C, D modify existing classes violating OCP.Final Answer:
Option C -> Option CQuick Check:
Adding new enums and classes externally respects OCP [check]
ElectricCar needing ChargingSpot in the current parking lot design, what is the minimum change that respects extensibility and avoids modifying existing classes?Solution
Step 1: Identify extensibility without modifying existing classes
Enums are extended with new constants; new subclassElectricCaris created without changing existing classes.Step 2: Update compatibility map to include new vehicle and spot types
SpotCompatibility.COMPATIBILITY_MAPis updated to reflect which spots fitElectricCar, preserving OCP.Final Answer:
Option D -> Option DQuick Check:
Extend enums + new subclass + update compatibility map -> no existing class modified [check]
