Practice
private constructor and a static synchronized getInstance() method returning the single instance. Which design pattern is this, and why is it the correct choice here?Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single instance with thread safety, matching Singleton pattern characteristics.Step 2: Eliminate other patterns
Factory Method controls subclass instantiation, Builder separates construction from representation, Prototype clones objects; none enforce single instance with thread safety.Final Answer:
Option C -> Option CQuick Check:
Private constructor + synchronized static getInstance() -> Singleton pattern [check]
Solution
Step 1: Understand OCP and DIP
OCP requires extending behavior without modifying existing code; DIP requires depending on abstractions, not concretions.Step 2: Evaluate options
Modify the existing FeeHandler class to add weekend fee logic inside its handleRequest() method. modifies existing class, violating OCP; Create a new WeekendFeeHandler implementing the FeeStrategy interface and add it to the chain without changing existing handlers. adds new handler, respecting OCP and DIP; Add a boolean flag isWeekend to FeeHandler and branch logic based on it. adds flag inside existing handler, violating OCP; Create a subclass WeekendFeeHandler extending FeeHandler and override handleRequest() with weekend logic. subclasses but may cause tight coupling and violates DIP if chain depends on concrete classes.Final Answer:
Option D -> Option DQuick Check:
New handler class added without modifying existing code [check]
WeekendPermissionStrategy that changes file access permissions on weekends without modifying existing classes. Which SOLID principle guides the minimum change, and what is the correct approach?Solution
Step 1: Identify the SOLID principle for extensibility without modification
The Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Apply OCP by adding a new WeekendPermissionStrategy implementing existing PermissionStrategy interface
This allows adding weekend-specific logic without changing existing Directory or File classes.Final Answer:
Option C -> Option CQuick Check:
New strategy class + interface -> OCP compliance [check]
FeeStrategy interface, adding a WeekendFeeStrategy requires which minimal change to comply with SOLID principles?Solution
Step 1: Identify applicable SOLID principle
The Open-Closed Principle (OCP) requires classes to be open for extension but closed for modification.Step 2: Evaluate options for minimal change and OCP compliance
Adding a new class implementingFeeStrategy(Add a newWeekendFeeStrategyclass implementingFeeStrategywithout changing existing classes.) is ideal, but if the design requires weekend logic in all strategies, adding a flag and conditionals violates OCP by modifying all classes.Final Answer:
Option D -> Option DQuick Check:
Adding flag and conditionals modifies existing classes -> violates OCP โ
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclasses allows extension without changing existing class code, preserving OCP.Step 2: Update compatibility externally
Updating SpotCompatibility map outside existing classes avoids modifying their code and prevents class explosion or god-class logic.Final Answer:
Option D -> Option DQuick Check:
Extend enums + subclass + external compatibility update -> minimal change [check]
