Practice
NotificationFactory class has a static method that returns different Notification subclasses (e.g., EmailNotification, SMSNotification) based on input parameters. Which design pattern is applied here and why is it the correct choice?Solution
Step 1: Identify the role of NotificationFactory
The factory method returns different Notification subclasses based on input parameters, encapsulating object creation logic.Step 2: Eliminate other patterns
Builder is for stepwise construction, Singleton ensures single instance, Decorator adds behavior dynamically; none match the described creation logic.Final Answer:
Option B -> Option BQuick Check:
Factory pattern encapsulates subclass instantiation based on input [check]
Board class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is it the correct choice?Solution
Step 1: Identify the role of private constructor and static synchronized method
The private constructor prevents external instantiation, and the synchronizedgetInstance()ensures thread-safe single instance creation.Step 2: Match to design pattern intent
Singleton pattern ensures exactly oneBoardinstance exists, which is critical to maintain consistent game state.Final Answer:
Option A -> Option AQuick Check:
Singleton controls single instance creation with thread safety [check]
Vehicle abstraction for a parking system, should Vehicle be an abstract class or an interface? Consider that vehicles have state like license plate and behavior like park().Solution
Step 1: Analyze state and behavior requirements
Vehicles have shared state (e.g., license plate) and common behavior (e.g., park). Abstract classes can hold state and provide default method implementations.Step 2: Eliminate interface options
Interfaces cannot hold state (prior to Java 8 default methods) and rely on external storage for state, which complicates design. Abstract class better encapsulates state and behavior.Final Answer:
Option A -> Option AQuick Check:
Shared state + default behavior -> abstract class [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]
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]
