Bite-sized, standalone interview questions and answers for Design Patterns. Read 5-10 per sitting. Each answer is 2-4 sentences max and stands on its own.

73 questions shown. Filter by keyword or difficulty below.

Topic: Strategy pattern (Order 1)

An enum + switch at the call site couples every caller to every possible behavior — adding a new strategy means editing code you don’t own. Strategy puts each algorithm behind a common interface so the caller depends only on the abstraction, and new behaviors are added by creating a new class, not editing any caller.

Polly’s contract is a single method (ExecuteCore) operating on an opaque Func callback — the strategy has no idea what it’s retrying or timing out (HTTP, database, gRPC). This makes one strategy reusable across every kind of call in a codebase, whereas a textbook interface often leaks domain-specific parameters.

ShouldHandle is injected separately from the retry loop mechanism — this is a second, smaller instance of Strategy-within-Strategy. The mechanism (retry N times) is decoupled from the policy (which outcomes count as failures), so the same retry loop can be configured with different failure-detection logic per call site.

You’ve reintroduced the switch statement one layer deeper — the context is no longer strategy-agnostic, and adding a new strategy now requires editing the context. The pattern’s entire value (open for extension, closed for modification) is defeated.

Making the interface too broad — if ExecuteCore had parameters specific to retries (attempt count, delay) or specific to timeouts (duration), strategies would need to know about each other’s concerns. The contract must be genuinely minimal (one method, opaque callback) or the strategies become coupled.

When behaviors need to be swapped at runtime, composed in different combinations, or shared across unrelated call sites. Subclassing bakes behavior into the class hierarchy at compile time; Strategy lets you inject different algorithms per instance, or even per call, without inheritance.

Topic: Factory Method & Abstract Factory (Order 2)