Practice
ReservationManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here 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 instance is created and accessed globally.Step 2: Why Singleton fits ReservationManager
ReservationManager must coordinate all reservations centrally to avoid conflicts; multiple instances would cause inconsistent state.Final Answer:
Option D -> Option DQuick Check:
Private constructor + static synchronized getInstance() -> Singleton pattern [check]
User and Message classes for the messaging system, should User be an abstract class or an interface, and why?Solution
Step 1: Analyze if User has state or just behavior
User holds common state such as userId and username, which requires fields and possibly default implementations.Step 2: Determine if abstract class or interface fits state and behavior
Abstract class supports shared state and partial implementation, while interface cannot hold instance state.Final Answer:
Option C -> Option CQuick Check:
User has common state -> abstract class fits better than interface [check]
Ball entity be modeled as an abstract class or an interface? Consider that Ball has state like ballNumber and behavior like calculateRuns(). Which is the best choice and why?Solution
Step 1: Analyze Ball's state and behavior
Ball has state (ballNumber) and behavior (calculateRuns), so it needs to store data and provide common logic.Step 2: Abstract class vs interface for state
Interfaces cannot hold instance state; abstract classes can hold state and provide shared implementation.Final Answer:
Option A -> Option AQuick Check:
Stateful entity with behavior -> abstract class [check]
WeekendFeeStrategy to calculate plugin usage fees on weekends without modifying existing plugin classes. Which SOLID principle guides the minimum change, and what is the correct approach?Solution
Step 1: Identify the SOLID principle for adding new behavior without modifying existing code
The Open-Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Apply OCP by creating a new WeekendFeeStrategy implementing FeeStrategy interface
This allows adding weekend fee logic without changing existing plugin or fee classes.Final Answer:
Option A -> Option AQuick Check:
OCP: extend with new strategy class, no existing class changes [check]
WeekendFeeStrategy pricing rule in the Workflow Engine's fee calculation, which minimal change respects SOLID principles?Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires classes to be open for extension but closed for modification.Step 2: Apply OCP to fee strategy
Adding a new WeekendFeeStrategy class extends behavior without modifying existing FeeCalculator, respecting OCP and Dependency Inversion Principle.Final Answer:
Option B -> Option BQuick Check:
New strategy class + no FeeCalculator change -> OCP respected [check]
