Practice
NotificationChannel be an abstract class or an interface? Consider that each channel has state like connection status and common methods like connect() and send(). Why?Solution
Step 1: Analyze state and behavior requirements
NotificationChannel needs to hold connection status (state) and provide common method implementations.Step 2: Choose between interface and abstract class
Interfaces cannot hold instance state; abstract classes can hold state and provide default behavior, making abstract class the correct choice.Final Answer:
Option A -> Option AQuick Check:
Abstract class supports shared state and default methods [check]
WeekendFeeStrategy to calculate parking fees differently on weekends. Given the current design uses a FeeStrategy interface, what is the minimum change to comply with SOLID principles?Solution
Step 1: Identify SOLID principle
Open-Closed Principle (OCP) requires classes to be open for extension but closed for modification.Step 2: Apply OCP
Creating a newWeekendFeeStrategyclass extends functionality without modifying existing code, adhering to OCP and Dependency Inversion Principle.Final Answer:
Option C -> Option CQuick Check:
New strategy class -> extend without modifying existing -> OCP [check]
FeeCalculator class. To add a WeekendFeeStrategy without modifying existing classes, which SOLID principle applies and what is the minimal correct change?Solution
Step 1: Identify applicable SOLID principles
Open-Closed Principle (OCP) requires FeeCalculator not be modified for new fee types. Dependency Inversion Principle (DIP) requires FeeCalculator depend on abstractions.Step 2: Apply correct design
Create WeekendFeeStrategy implementing FeeStrategy interface. Inject FeeStrategy into FeeCalculator. This adds new behavior without modifying existing code.Final Answer:
Option C -> Option CQuick Check:
New strategy class + DI -> OCP and DIP respected [check]
WeekendFeeStrategy to calculate weekend parking fees 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
Open/Closed Principle (OCP) requires adding new functionality without modifying existing code.Step 2: Evaluate approaches
Creating a new FeeStrategy implementation and injecting it respects OCP and DIP; modifying ParkingLot or existing strategies violates OCP or mixes responsibilities.Final Answer:
Option B -> Option BQuick Check:
New FeeStrategy class + injection -> OCP and DIP respected [check]
ElectricCar vehicles that require ChargingSpot notifications. What is the minimum design change to support this without modifying existing classes?Solution
Step 1: Identify extensibility requirements
New vehicle and spot types must be added without modifying existing classes to respect OCP.Step 2: Apply minimal changes
Adding new enum values and creating new subclasses allows extension without modifying existing class code; updating compatibility map centralizes new logic.Step 3: Eliminate other options
Options B, C, and D modify existing classes or factory logic, violating OCP and risking class explosion or god-class anti-pattern.Final Answer:
Option C -> Option CQuick Check:
New enums and subclasses extend design without modifying existing classes [check]
