Practice
State interface defines methods like selectProduct(), insertMoney(), and dispense(). Concrete classes like IdleState, HasMoneyState, and SoldState implement this interface. Which design pattern is applied here and why is it the correct choice?Solution
Step 1: Identify the role of State interface and concrete states
TheStateinterface defines behavior that varies depending on the vending machine's current state. Concrete classes implement these behaviors and handle transitions.Step 2: Match pattern intent to scenario
The State pattern encapsulates state-specific behavior and transitions, allowing the vending machine to change behavior dynamically without complex conditionals.Final Answer:
Option D -> Option DQuick Check:
State pattern encapsulates state behavior and transitions [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]
WeekendFeeStrategy to the current parking fee system requires which minimum change to comply with SOLID principles?Solution
Step 1: Identify applicable SOLID principle
The Open/Closed Principle (OCP) requires that classes be open for extension but closed for modification.Step 2: Evaluate options against OCP
Create a newWeekendFeeStrategyclass implementingFeeStrategywithout modifying existing classes. respects OCP by adding a new class without modifying existing ones. Add a boolean flagisWeekendinParkingLotand conditionally apply fees inside existing strategies. violates OCP by modifyingParkingLotand existing strategies with conditional logic.Final Answer:
Option A -> Option AQuick Check:
New strategy class added, existing code unchanged -> OCP respected [check]
ElectricCar profiles and ChargingSpot types without modifying existing classes. What is the minimum change to the current Java design to support this extensibility?Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclasses allows extension without changing existing class code, preserving OCP.Step 2: Update compatibility map externally
Updating SpotCompatibility.COMPATIBILITY_MAP to include new types supports new logic without modifying existing classes.Final Answer:
Option C -> Option CQuick Check:
New enums + subclass + external map update -> no existing class changes [check]
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclasses allows extension without changing existing class code, preserving OCP.Step 2: Update compatibility externally
Updating SpotCompatibility map outside existing classes avoids modifying their code and prevents class explosion or god-class logic.Final Answer:
Option D -> Option DQuick Check:
Extend enums + subclass + external compatibility update -> minimal change [check]
