Practice
getInstance() method returning a single instance. Which pattern is applied and why is this the CORRECT use case?Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single shared instance with thread safety.Step 2: Eliminate other creational patterns
Factory Method creates objects but doesn't enforce single instance; Builder constructs complex objects but not singleton; Prototype clones instances, not restricts to one.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized static getter -> Singleton pattern [check]
FeeCalculator class. To add a new WeekendFeeStrategy without modifying existing code, which SOLID principle applies and what is the minimum change required?Solution
Step 1: Identify the SOLID principle for extensibility
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: How to apply OCP here
Create a new class WeekendFeeStrategy implementing FeeStrategy interface; FeeCalculator uses polymorphism to apply different strategies without modification.Final Answer:
Option A -> Option AQuick Check:
New strategy class + polymorphism -> OCP compliance [check]
WeekendFeeStrategy for job posting fees without modifying existing classes. According to SOLID principles, what is the minimum change required to support this?Solution
Step 1: Identify the SOLID principle involved
The Open-Closed Principle (OCP) requires that classes be open for extension but closed for modification.Step 2: Apply OCP to fee strategy
Creating a newWeekendFeeStrategyclass implementing the existingFeeStrategyinterface allows extension without modifying existing classes.Final Answer:
Option D -> Option DQuick Check:
New FeeStrategy class extends behavior without modifying existing code [check]
WeekendFeeStrategy to a parking system using the Observer pattern. According to SOLID principles, what is the minimum change required to support this new pricing without modifying existing classes?Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires adding new behavior without modifying existing code.Step 2: Evaluate options
Modify the existing FeeStrategy class to add weekend logic with an if-else block. modifies existing class, violating OCP; Create a new WeekendFeeStrategy class implementing FeeStrategy and register it as an observer without changing existing code. adds new class but registering as observer for fee strategy is not typical; Create a subclass of the existing FeeStrategy with weekend logic and replace all references. requires replacing references, increasing coupling.Step 3: Add a boolean flag in the Subject to indicate weekend and notify observers to adjust fees accordingly. adds a weekend flag in Subject and notifies observers to adjust fees, allowing extension without modifying existing classes.
Final Answer:
Option C -> Option CQuick Check:
Weekend flag in Subject triggers observers -> new behavior without modifying existing classes [check]
Solution
Step 1: Identify extensibility without modifying existing classes
Enums VehicleType and SpotType can be extended with new constants without changing existing class logic. Creating ElectricCar subclass extends Vehicle cleanly.Step 2: Update SpotCompatibility map externally
Maintaining compatibility mapping outside existing classes avoids modifying them, preserving OCP and avoiding class explosion.Final Answer:
Option A -> Option AQuick Check:
Extend enums + subclass + external compatibility map -> minimal change [check]
