Practice
1. In a Card Game Framework, the
Deck class has a private constructor and a static synchronized getInstance() method returning the single deck instance. Which design pattern is applied here and why is this the correct choice?easy
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, matching Singleton pattern characteristics.Step 2: Why Singleton fits this scenario
In a card game, having a single deck instance avoids inconsistencies and duplication, ensuring all players draw from the same deck state.Final Answer:
Option B -> Option BQuick Check:
Private constructor + static synchronized getInstance() -> Singleton [check]
Hint: Private constructor + static getInstance() -> Singleton [check]
Common Mistakes:
2. In a notification system, a
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?easy
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]
Hint: Factory creates objects based on input type [check]
Common Mistakes:
3. In the Ride Booking system, the
FareCalculator 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?easy
Solution
Step 1: Identify the pattern from constructor and method signatures
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single shared instance, matching Singleton pattern characteristics.Step 2: Why Singleton fits FareCalculator
FareCalculator needs consistent fare rules and shared state (e.g., pricing configs). Singleton avoids multiple inconsistent instances and ensures thread-safe access.Final Answer:
Option C -> Option CQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
Hint: Private constructor + static getInstance() = Singleton [check]
Common Mistakes:
4. You want to add a
WeekendFeeStrategy to calculate message delivery fees differently on weekends. According to SOLID principles, what is the minimum change required in the current design?medium
Solution
Step 1: Identify the SOLID principle involved
Open-Closed Principle (OCP) requires adding new behavior without modifying existing classes.Step 2: Evaluate options against OCP
Add a boolean flagisWeekendinMessageand handle fees there. adds weekend logic inside Message, violating SRP and mixing concerns; Modify the existingFeeCalculatorclass to add weekend logic with an if-else block. modifies existing class, violating OCP; Create a subclassWeekendFeeCalculatorextendingFeeCalculatorand override fee calculation. creates subclass but may cause class explosion; Create a newWeekendFeeStrategyclass implementing the existing FeeStrategy interface and inject it where needed. adds new strategy class implementing FeeStrategy, adhering to OCP and DIP.Final Answer:
Option A -> Option AQuick Check:
New strategy class -> OCP and DIP respected [check]
Hint: New strategy class -> Open-Closed Principle [check]
Common Mistakes:
5. To support ElectricCar and ChargingSpot in the existing parking system using Chain of Responsibility, what is the minimum change that respects extensibility and avoids modifying existing classes?
hard
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot should not require modifying existing classes to avoid breaking OCP.Step 2: Evaluate options
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility.COMPATIBILITY_MAP accordingly. modifies enums and compatibility map, changing existing code; Modify Vehicle and Spot classes to add boolean isElectric and isChargingSpot flags, then update existing handlers to check these flags. adds flags and updates handlers without changing existing class hierarchies; Create a new ElectricCarHandler in the chain that handles electric cars and charging spots, modifying the chain setup to include it. modifies chain setup, which is acceptable but may require modifying chain initialization; Add a new method in Vehicle interface for charging compatibility and implement it in ElectricCar, modifying existing handlers to call it. modifies Vehicle interface, breaking existing implementations.Step 3: Choose minimal invasive approach
Modify Vehicle and Spot classes to add boolean isElectric and isChargingSpot flags, then update existing handlers to check these flags. adds minimal changes with flags and updates handlers, avoiding class explosion or interface changes.Final Answer:
Option B -> Option BQuick Check:
Flags added, existing classes unchanged, handlers updated [check]
Hint: Add flags and update handlers, avoid modifying classes [check]
Common Mistakes:
