Which statement best describes the Strategy pattern?

Get ready for your Object‑Oriented Programming Test. Use flashcards and multiple-choice questions. Each question includes hints and explanations. Prepare for your exam today!

Multiple Choice

Which statement best describes the Strategy pattern?

Explanation:
The idea being tested is that you can change an object’s behavior by swapping out its algorithm at runtime through a shared interface. In the Strategy pattern you define a family of algorithms, implement each one as a concrete strategy that all implements the same interface, and let a context hold a reference to a strategy. The context delegates work to the current strategy, so you can swap the strategy object at runtime to alter behavior without touching the context code. This keeps behavior flexible and extensible: adding a new algorithm just means adding a new strategy class that implements the interface, and the rest of the code can use it without modification. The other options describe different concepts—creating objects without exposing creation logic points to a Factory pattern, claiming it isn’t related to runtime behavior misstates the pattern’s purpose, and persisting data across sessions is about storage, not algorithm selection. For clarity, imagine a routing feature that can switch between shortest, fastest, or eco routes by swapping the strategy at runtime, all behind the same interface.

The idea being tested is that you can change an object’s behavior by swapping out its algorithm at runtime through a shared interface. In the Strategy pattern you define a family of algorithms, implement each one as a concrete strategy that all implements the same interface, and let a context hold a reference to a strategy. The context delegates work to the current strategy, so you can swap the strategy object at runtime to alter behavior without touching the context code. This keeps behavior flexible and extensible: adding a new algorithm just means adding a new strategy class that implements the interface, and the rest of the code can use it without modification. The other options describe different concepts—creating objects without exposing creation logic points to a Factory pattern, claiming it isn’t related to runtime behavior misstates the pattern’s purpose, and persisting data across sessions is about storage, not algorithm selection. For clarity, imagine a routing feature that can switch between shortest, fastest, or eco routes by swapping the strategy at runtime, all behind the same interface.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy