Practice
Solution
Step 1: Understand responsibilities
The Vehicle class only knows about itself, not allocation rules. The ParkingSpot class represents a spot but doesn't decide allocation. The ParkingLot manages overall state but delegating allocation logic to a dedicated component improves modularity.Step 2: Recognize design pattern role
The Factory or Strategy pattern encapsulates allocation logic, allowing easy extension and modification without changing core classes.Final Answer:
Option D -> Option DQuick Check:
Allocation logic centralized -> easier to maintain and extend [OK]
- Assigning allocation responsibility to Vehicle or ParkingSpot classes
- Putting all logic inside ParkingLot class leading to tight coupling
Solution
Step 1: Identify extension vs modification
Adding new payment methods should extend behavior without modifying existing classes.Step 2: Evaluate subclassing with common interface
Creating subclasses for each payment method allows extension by adding new classes, adhering to OCP.Step 3: Why other options fail
Adding new conditional branches inside the existing payment processor class for each new payment method and modifying existing code violate the 'closed for modification' part. Using global variables to switch between payment methods at runtime uses global state, which is unrelated and error-prone.Final Answer:
Option A -> Option AQuick Check:
Extension via polymorphism, no modification of existing code.
- Thinking adding conditionals is extension
- Believing modifying existing code is acceptable if tested
- Confusing global state management with OCP
Solution
Step 1: Understand the Diamond Problem
It arises when a class inherits from two classes that share a common ancestor, causing ambiguity and duplication.Step 2: Virtual inheritance role
Virtual inheritance in C++ ensures only one instance of the base class exists, resolving duplication.Step 3: Role of MRO
MRO is critical in languages like Python to determine method lookup order and resolve ambiguity.Step 4: Identify incorrect statement
Method Resolution Order (MRO) is irrelevant to resolving the Diamond Problem. incorrectly claims MRO is irrelevant, which is false.Final Answer:
Option C -> Option CQuick Check:
MRO is essential for resolving method lookup in diamond inheritance scenarios.
- Ignoring MRO's role in method lookup
- Confusing virtual inheritance with method resolution
- Assuming diamond problem only causes ambiguity, not duplication
Solution
Step 1: Understand dynamic add/remove requirement
Need to add or remove decorators at runtime without subclass explosion or code changes.Step 2: Identify suitable data structure and pattern modification
Using a stack to track decorators allows pushing and popping decorators, updating the wrapped object reference accordingly.Step 3: Evaluate other options
Inheritance and flags lead to rigidity; caching all combinations is infeasible for many decorators.Final Answer:
Option C -> Option CQuick Check:
Stack structure supports dynamic decorator management efficiently [OK]
- Trying to use inheritance for dynamic behavior
- Modifying base class breaks open-closed principle
Solution
Step 1: Identify extensibility needs
Pricing rules vary by multiple factors and may change frequently.Step 2: Evaluate design options
Embedding logic in ParkingSpot or ParkingLot leads to rigid, hard-to-maintain code. Subclass explosion is unmanageable.Step 3: Recognize Strategy pattern benefits
Strategy encapsulates pricing algorithms, allowing dynamic assignment and easy extension without modifying existing classes.Final Answer:
Option A -> Option AQuick Check:
Strategy pattern supports flexible, maintainable pricing logic [OK]
- Embedding complex logic inside core classes
- Using inheritance for every pricing variant
