Practice
if-else statements to handle different payment methods like credit card, UPI, and net banking. The system needs to be extended frequently with new payment methods without modifying existing code. Which design approach best addresses this requirement?Solution
Step 1: Understand the problem of frequent extension
The system requires adding new payment methods without modifying existing code, which violates the open-closed principle if usingif-elsechains.Step 2: Identify the design pattern that encapsulates behaviors
The strategy pattern encapsulates each payment method in its own class implementing a common interface, allowing easy extension by adding new classes without changing existing code.Final Answer:
Option B -> Option BQuick Check:
Strategy pattern replaces conditionals with polymorphism [OK]
- Thinking recursion or greedy algorithms solve extensibility here
Solution
Step 1: Recall Facade's purpose
Facade simplifies complex subsystems by providing a unified interface.Step 2: Analyze trade-offs
While Facade simplifies usage, it can hide advanced features, limiting flexibility.Step 3: Evaluate other options
A is incorrect because Facade reduces coupling by hiding subsystem details. C is incorrect; Facade's overhead is minimal. D is wrong; Facade does not require changing subsystems.Final Answer:
Option A -> Option A
- Believing Facade increases coupling instead of reducing it
- Assuming Facade adds heavy runtime overhead
- Thinking Facade requires modifying subsystems
Solution
Step 1: Evaluate each option's validity
Composition always leads to more complex code and harder maintenance than inheritance is false; composition often improves maintainability. Composition prevents code reuse since behaviors cannot be shared is false; composition promotes code reuse via behavior objects. Composition forces tight coupling between composed objects is false; composition reduces coupling by separating concerns.Step 2: Understand the trade-off
Composition introduces more objects and delegation layers, which can add runtime overhead and complexity in object management.Step 3: Confirm correct trade-off
Composition can increase the number of objects and indirection, potentially impacting performance correctly identifies the potential performance and complexity cost of increased indirection.Final Answer:
Option B -> Option BQuick Check:
Composition trades off some runtime overhead for flexibility and maintainability.
- Believing composition always simplifies code without cost
- Thinking composition prevents code reuse
Solution
Step 1: Encapsulation of Player position
Correct: Position changes only via controlled methods to maintain validity.Step 2: Dice class design
Incorrect: Exposing internal RNG breaks encapsulation and risks inconsistent behavior.Step 3: Board class encapsulation
Correct: Board hides snake/ladder details, exposing only necessary interfaces.Step 4: GameController encapsulation
Correct: It manages rules and state transitions to keep game consistent.Final Answer:
Option C -> Option CQuick Check:
Only Dice exposing internal RNG violates encapsulation principles.
- Thinking exposing RNG aids testing without drawbacks
- Confusing encapsulation with just data hiding
- Assuming all classes should expose internals for flexibility
Solution
Step 1: Understand the Diamond Problem
Diamond Problem arises when a class inherits from two classes that share a common ancestor, causing ambiguity.Step 2: Evaluate Multiple inheritance always leads to ambiguous method calls that cannot be resolved.
Multiple inheritance can cause ambiguity, but languages use MRO to resolve it, so it is not always unresolved.Step 3: Evaluate Multiple inheritance eliminates the need for Method Resolution Order (MRO).
MRO is essential in multiple inheritance to resolve method calls, so multiple inheritance does not eliminate MRO.Step 4: Evaluate Multiple inheritance reduces code reuse compared to single inheritance.
Multiple inheritance generally increases code reuse by combining features from multiple classes.Step 5: Correct trade-off
Using multiple inheritance can increase complexity and make the class hierarchy harder to understand and maintain. correctly identifies that multiple inheritance increases complexity and can make hierarchies harder to maintain.Final Answer:
Option D -> Option DQuick Check:
Complexity and maintainability are key trade-offs in multiple inheritance.
- Believing multiple inheritance always causes irresolvable ambiguity
- Thinking MRO is unnecessary with multiple inheritance
- Assuming multiple inheritance reduces code reuse
