Practice
Logger class with a private constructor and a static synchronized getInstance() method returning the single instance. Which design pattern is applied here and why is it the correct choice?Solution
Step 1: Identify the pattern from private constructor and static getInstance()
The private constructor prevents external instantiation, and the static synchronized getInstance() ensures a single shared instance, which matches the Singleton pattern.Step 2: Why Singleton is correct here
Logging frameworks require a single global Logger instance to coordinate log output consistently. Factory, Builder, and Prototype patterns do not enforce single instance semantics.Final Answer:
Option D -> Option DQuick Check:
Private constructor + static synchronized getInstance() -> Singleton [check]
Track abstraction for the music library, should Track be an abstract class or an interface? Consider that each Track has state like duration and title, and behavior like play() and pause().Solution
Step 1: Analyze state and behavior requirements
Track has shared state (duration, title) and common behavior (play, pause) that can be implemented once.Step 2: Abstract class vs interface for state and behavior
Abstract class can hold state fields and provide default method implementations, while interfaces cannot hold instance state.Final Answer:
Option C -> Option CQuick Check:
State + default behavior -> abstract class [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]
WeekendFeeStrategy to calculate parking fees on weekends without modifying existing classes. Which SOLID principle guides this design and what is the minimum change?Solution
Step 1: Identify applicable SOLID principles
Open/Closed Principle (OCP) requires extending behavior without modifying existing code; Dependency Inversion Principle (DIP) requires depending on abstractions likeFeeStrategy.Step 2: Apply minimal change preserving OCP and DIP
ImplementWeekendFeeStrategyas a new class implementingFeeStrategyand inject it where needed, avoiding changes to existing classes.Final Answer:
Option A -> Option AQuick Check:
New strategy class + injection -> OCP and DIP preserved [check]
Solution
Step 1: Identify extensibility without modifying existing classes
To comply with OCP and avoid class explosion, new types should be added via enums and new subclasses without changing existing code.Step 2: Explain why Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map externally. fits minimal change
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map externally. adds new enum values and creates a new ElectricCar subclass, updating compatibility externally, preserving existing classes unchanged. Options B, C, and D modify existing classes or create subclasses that duplicate logic, violating OCP or SRP.Final Answer:
Option D -> Option DQuick Check:
New enums + subclass + external compatibility update -> minimal change [check]
