Practice
Solution
Step 1: Understand Proxy's role
Proxy controls access and may delay creation of the real service (lazy initialization).Step 2: Trace the call flow
Client calls Proxy -> Proxy checks access -> Proxy creates real service if not already created -> Proxy forwards call -> Real service executes.Step 3: Eliminate incorrect options
B is incorrect because Proxy usually adds control logic before forwarding. C is incorrect as Proxy typically does not modify parameters (that's Decorator or Adapter). D describes Facade, not Proxy.Final Answer:
Option A -> Option A
- Assuming Proxy always forwards calls immediately without checks
- Confusing Proxy with Facade's simplification role
- Thinking Proxy modifies request parameters
Tea().prepare_recipe()?Solution
Step 1: Trace prepare_recipe steps
Calls boil_water (prints 'Boil water'), then brew (prints 'Steep tea bag'), then pour_in_cup (prints 'Pour into cup').Step 2: Check hook method customer_wants_condiments
Tea overrides it to return False, so add_condiments is skipped.Final Answer:
Option A -> Option AQuick Check:
Condiments skipped due to hook returning False [OK]
- Assuming condiments always added
- Mixing order of steps
Solution
Step 1: Identify stack initialization issue
Stack is initialized with children in original order, not reversed, causing traversal order reversal.Step 2: Confirm impact on traversal order
Without reversing, popping from stack yields children in reverse order, breaking expected traversal.Final Answer:
Option A -> Option AQuick Check:
Reversing children on stack initialization fixes traversal order [OK]
- Forgetting to reverse children on stack push
- Misplacing hasNext() check
Solution
Step 1: Understand SRP effect
SRP encourages splitting responsibilities, which can increase the number of classes.Step 2: Trade-off analysis
More classes improve modularity but can make the system harder to navigate and understand due to fragmentation.Step 3: Evaluate other options
It always reduces code complexity and never increases the number of classes. is false because SRP can increase class count; It forces all classes to be tightly coupled to maintain cohesion. is false because SRP reduces coupling; It eliminates the need for interfaces or abstractions in the system. is false because SRP does not remove the need for abstractions.Final Answer:
Option C -> Option CQuick Check:
More classes can complicate system navigation -> trade-off of SRP.
- Believing SRP always simplifies codebase size.
- Confusing coupling with cohesion effects.
- Assuming SRP removes need for design patterns.
Solution
Step 1: Identify overridden methods
Tea overrides prepare_recipe, which breaks the template method pattern by duplicating and changing the algorithm flow.Step 2: Understand impact
Overriding the template method in subclass bypasses the base class skeleton, causing inconsistent behavior and code duplication.Final Answer:
Option A -> Option AQuick Check:
Template method must not be overridden by subclasses [OK]
- Thinking overriding abstract methods is bug
- Ignoring hook method usage
