Practice
Solution
Step 1: Identify the problem
The legacy system's interface is incompatible with the new platform.Step 2: Understand pattern intents
Adapter converts one interface to another, enabling integration without changing legacy code. Facade simplifies a complex subsystem but doesn't change interfaces. Proxy controls access, not interface compatibility. Decorator adds behavior dynamically, unrelated to interface mismatch.Final Answer:
Option B -> Option BQuick Check:
Adapter is the go-to pattern for interface incompatibility issues.
- Confusing Facade with Adapter because both provide a new interface
- Thinking Proxy changes interfaces rather than controlling access
- Assuming Decorator handles interface incompatibility
Solution
Step 1: Understand the role of Dice
The Dice only generates a random number; it does not manage state transitions.Step 2: Consider Player class responsibilities
Player holds position but should not decide how to update it considering snakes or ladders.Step 3: Analyze Board class role
Board knows snakes and ladders but does not manage player state transitions directly.Step 4: Role of GameController
GameController coordinates dice roll, queries Board for snakes/ladders, and updates Player position accordingly.Final Answer:
Option D -> Option DQuick Check:
GameController centralizes state transitions, ensuring separation of concerns.
- Thinking Dice manages player position
- Assuming Player updates position without Board's input
- Believing Board directly changes player state
Solution
Step 1: Identify reasons to change
User account management changes when user data or authentication changes; email notifications change when messaging or delivery requirements change.Step 2: Apply SRP
Since these reasons to change differ, they should be separated into different classes to avoid coupling unrelated changes.Step 3: Evaluate other options
Options A, C, and D combine responsibilities, increasing coupling and reducing cohesion, violating SRP.Final Answer:
Option A -> Option AQuick Check:
Separate classes for distinct reasons to change -> SRP compliant.
- Assuming related domain means same responsibility.
- Combining functionalities to reduce class count.
- Embedding multiple responsibilities for convenience.
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: Singleton Board pattern
Incorrect: Singleton would cause shared state across games, leading to conflicts.Step 2: Independent GameController and Player per game
Correct: Isolates state per game, preventing interference.Step 3: Sharing Dice instance
Incorrect: Shared Dice can cause race conditions or inconsistent rolls.Step 4: Global player positions
Incorrect: Global state breaks isolation and causes bugs.Final Answer:
Option A -> Option AQuick Check:
Isolating state per game instance is key for concurrency safety.
- Using singleton for shared components without considering concurrency
- Sharing mutable objects like Dice across threads
- Centralizing state globally ignoring isolation
