Design Patterns in Python
- 18Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is the main purpose of the Strategy design pattern?
18 questions
- 1
What is the main purpose of the Strategy design pattern?
Model answer
The Strategy pattern allows replacing one algorithm with another without the system that uses the algorithm knowing about the change.
Explanation
The pattern decouples algorithms from the client, enabling runtime interchangeability.
- 2
Describe the classic object-oriented implementation of the Strategy pattern as presented in the content.
Model answer
An abstract class defines the algorithm interface, concrete subclasses implement specific algorithms, and a context class holds a reference to a strategy and uses it.
Explanation
This uses inheritance and polymorphism to achieve algorithm exchange.
- 3
How can the Strategy pattern be implemented using functions instead of classes?
Model answer
Define individual sorting functions (e.g., bubble_sort, quicksort) and a context function that takes the sorting function as an argument.
Explanation
This avoids class overhead and makes code simpler.
- 4
What key functional programming technique is used in the functional version of the Strategy pattern?
Model answer
Passing a function as an argument to another function.
Explanation
This technique allows the context function to accept any compatible sorting function.
- 5
What is the main advantage of the functional Strategy implementation over the OO version according to the content?
Model answer
It reduces code size (e.g., from 65 lines to 50 lines), resulting in simpler code.
Explanation
Functions often lead to fewer lines of code compared to classes.
- 6
What problem does the Observer design pattern solve?
Model answer
It allows objects that change to notify other objects (observers) without being tightly coupled to them.
Explanation
The pattern decouples the subject from its dependents.
- 7
Describe the classic object-oriented implementation of the Observer pattern as shown.
Model answer
An abstract Observer class with an update method, concrete Observers implement it, and a Subject maintains a list of observers and has attach/detach/notify methods.
Explanation
The Subject only knows observers through the abstract interface.
- 8
How can the Observer pattern be implemented functionally?
Model answer
Define observer functions and a notify function that receives a list of observer functions and calls each with data.
Explanation
Instead of observer objects, functions are used and passed in a list.
- 9
What data structure is used in the functional Observer example to hold the observer functions?
Model answer
A list of functions is passed as an argument to the notify function.
Explanation
Using a list allows multiple observers to be attached and notified.
- 10
How does the Observer pattern achieve decoupling between the subject and observers?
Model answer
The subject only knows that observers have an update method (or are callable functions), not their concrete types.
Explanation
This separates the change notification logic from the specific actions taken.
- 11
What is the purpose of the Template Method design pattern?
Model answer
It defines the skeleton of an algorithm in a method, allowing subclasses to override certain steps without changing the algorithm's structure.
Explanation
The pattern controls the overall flow while permitting customization.
- 12
In the classic OO Template Method pattern, what are required operations and hooks?
Model answer
Required operations must be implemented by subclasses; hooks have default implementations and can be optionally overridden.
Explanation
Hooks provide extension points without mandating implementation.
- 13
How can the Template Method pattern be implemented functionally?
Model answer
Create a template function that takes required operations and optional hooks as arguments, with default values for hooks.
Explanation
This achieves the same effect using function parameters instead of inheritance.
- 14
What additional functional technique is demonstrated in the second functional version of the Template Method pattern?
Model answer
Returning a function as a result (closure) to pre-apply certain arguments.
Explanation
The base_template_method returns a closure that captures the required operations.
- 15
How does partial function application from functools simplify the functional Template Method?
Model answer
It constructs a new function with some arguments already applied, so you don't need to pass the same operations repeatedly.
Explanation
Partial application reduces repetition when calling the template method many times.
- 16
What is the main reason the author argues that design patterns are not obsolete?
Model answer
Design patterns identify classes of problems (e.g., algorithm exchange, notification) that still occur; the implementation choice (classes or functions) is secondary.
Explanation
The value lies in recognizing problem categories, not in the specific OO implementation.
- 17
According to the content, what is more important than using the exact class-based implementation from the Gang of Four book?
Model answer
Being able to detect the problem category (e.g., algorithm exchange, notification) and handle it in your preferred way (functions or classes).
Explanation
Developers should focus on understanding the problem type rather than rigidly following the book's code.
- 18
Which functional programming techniques are highlighted as alternatives to class-based design patterns?
Model answer
Passing functions as arguments, returning functions as results (closures), and using partial function application.
Explanation
These techniques allow implementing the same patterns with less code.

Ask anything
We've read this whole study set, so questions about it get the best answers.