Design Patterns in Python
- 23Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is the primary purpose of the strategy design pattern?
What is the primary purpose of the strategy design pattern?
23 questions
- 1
What is the primary purpose of the strategy design pattern?
- A
To define a standard algorithm with replaceable parts
- B
To allow replacement of one algorithm with another without the system knowing
- C
To notify multiple objects of changes in another object
- D
To decouple an abstraction from its implementation
Correct answer
BTo allow replacement of one algorithm with another without the system knowing
Explanation
The strategy pattern's goal is to exchange algorithms at runtime without affecting the client code, as shown in the sorting example.
- A
- 2
In the class-based strategy pattern example, what method does the abstract base class define?
- A
A method that attaches observers to the subject
- B
A method that returns a new function from a closure
- C
A method that multiplies list elements by a random number
- D
A method that takes a list of integers and returns a sorted list
Correct answer
DA method that takes a list of integers and returns a sorted list
Explanation
The abstract sorting strategy class defines a method that receives a list of integers and returns a sorted list, as shown with bubble sort and quicksort.
- A
- 3
What technique is used in the functional version of the strategy pattern?
- A
Passing a function as an argument to another function
- B
Using inheritance to override a base class method
- C
Returning a function from a function
- D
Using functools.partial to pre-apply arguments
Correct answer
APassing a function as an argument to another function
Explanation
The functional strategy replaces the class with a context function that takes a sorting function as an argument, allowing algorithm exchange by passing different functions.
- A
- 4
How does the context class in the class-based strategy pattern use the strategy?
- A
It creates a new strategy instance each time it sorts
- B
It inherits from the strategy class
- C
It stores a strategy object and calls its sorting method
- D
It directly calls a global sorting function
Correct answer
CIt stores a strategy object and calls its sorting method
Explanation
The context class holds a reference to a strategy object and delegates the sorting operation to that object's method, enabling runtime strategy changes.
- A
- 5
Which of the following best describes the observer pattern?
- A
It encapsulates a request as an object, allowing parameterization of clients with different requests
- B
It allows an object to alter its behavior when its internal state changes
- C
It provides a way to access the elements of an aggregate object sequentially
- D
It defines a one-to-many dependency so that when one object changes state, all its dependents are notified
Correct answer
DIt defines a one-to-many dependency so that when one object changes state, all its dependents are notified
Explanation
The observer pattern establishes a subscription mechanism where subjects notify observers of state changes without coupling to concrete observer classes.
- A
- 6
In the observer pattern, what method do observers implement to receive updates?
- A
attach
- B
detach
- C
notify
- D
update
Correct answer
Dupdate
Explanation
The observer interface includes an update method that the subject calls on each observer when notifying them of changes, as shown in the video example.
- A
- 7
How does the subject in the observer pattern maintain decoupling from concrete observers?
- A
It only knows that observers have an update method
- B
It directly instantiates each observer by its class name
- C
It stores observers in a dictionary mapping event types to classes
- D
It uses inheritance to connect observers to the subject
Correct answer
AIt only knows that observers have an update method
Explanation
The subject interacts with observers solely through the abstract observer interface, knowing only that each observer has an update method, preserving loose coupling.
- A
- 8
In the functional version of the observer pattern, what is passed to the notify function?
- A
A class inheriting from Observer
- B
A dictionary mapping event types to functions
- C
A list of observer functions
- D
A single observer function
Correct answer
CA list of observer functions
Explanation
The functional observer pattern uses a list of update functions that are called with the data, simplifying the pattern by avoiding classes.
- A
- 9
Which of the following is a typical use case for the observer pattern?
- A
Sending email or UI notifications when an order is processed
- B
Defining a standard algorithm where subclasses override specific steps
- C
Replacing one sorting algorithm with another at runtime
- D
Creating a single instance of a class shared across the system
Correct answer
ASending email or UI notifications when an order is processed
Explanation
The observer pattern is ideal for scenarios where a subject (e.g., order system) needs to notify multiple observers (e.g., email service, UI) without tight coupling.
- A
- 10
What is the template method pattern used for?
- A
To encapsulate a request as an object, allowing parameterization of clients
- B
To provide a way to access elements of a collection without exposing its representation
- C
To allow an object to alter its behavior when its internal state changes
- D
To define the skeleton of an algorithm, allowing subclasses to redefine certain steps
Correct answer
DTo define the skeleton of an algorithm, allowing subclasses to redefine certain steps
Explanation
The template method pattern defines the overall algorithm structure in a base class or function, with specific steps left for subclasses or callbacks to implement.
- A
- 11
In the class-based template method, what are hooks?
- A
Methods that return a function for functional programming
- B
Methods with default implementations that can be optionally overridden
- C
Abstract methods that have no implementation
- D
Methods that must be implemented by subclasses
Correct answer
BMethods with default implementations that can be optionally overridden
Explanation
Hooks provide optional extension points in the template method; if not overridden, the default behavior is used, as shown with hook1 and hook2 in the example.
- A
- 12
In the functional version of the template method, how are optional hooks handled?
- A
They must always be provided as required arguments
- B
They are given default values in the function signature
- C
They are ignored if not provided
- D
They are implemented in a separate class
Correct answer
BThey are given default values in the function signature
Explanation
The functional template method assigns default values to hooks (e.g.,
lambda: None), making them optional for callers while still allowing them to be replaced. - A
- 13
What technique is demonstrated by creating a function that returns another function in the template method example?
- A
Using lambda functions
- B
Passing a function as an argument
- C
Returning a function as a result
- D
Using functools.partial
Correct answer
CReturning a function as a result
Explanation
The closure example creates a
base_template_methodthat returns atemplate_methodfunction, illustrating how returning functions can add flexibility by pre-applying fixed operations. - A
- 14
What does functools.partial do in the template method example?
- A
It constructs a new function with some arguments pre-applied
- B
It creates a closure that captures environment variables
- C
It converts a class method into a standalone function
- D
It returns a new function with all arguments pre-applied
Correct answer
AIt constructs a new function with some arguments pre-applied
Explanation
partialcreates a new function by fixing some arguments of an existing function, allowing the template method to be called without repeating the operations argument. - A
- 15
In the class-based template method, what is the effect of overriding hook1 to return False?
- A
Base operation three is not executed
- B
Base operation three is executed twice
- C
The template method stops early
- D
An error is raised
Correct answer
ABase operation three is not executed
Explanation
In the example, when hook1 returns False, the conditional in the template method skips base operation three, as seen in the output difference between concrete classes.
- A
- 16
According to the speaker, why are design patterns not obsolete in Python?
- A
Because Python does not support first-class functions
- B
Because they represent problem categories that developers need to recognize
- C
Because Python cannot implement design patterns without classes
- D
Because the Gang of Four book is still the only valid reference
Correct answer
BBecause they represent problem categories that developers need to recognize
Explanation
The speaker argues that design patterns are about identifying and solving classes of problems, and this remains relevant regardless of whether you implement them with classes or functions.
- A
- 17
What is the core idea of design patterns according to the speaker?
- A
They are about implementing code exactly as described in the GoF book
- B
They are about using inheritance and interfaces exclusively
- C
They are about reducing the number of lines of code
- D
They are about detecting problem classes and handling them in one's preferred way
Correct answer
DThey are about detecting problem classes and handling them in one's preferred way
Explanation
The speaker emphasizes that design patterns encapsulate reusable solutions to common design problems, and the implementation (classes vs. functions) is secondary to recognizing the problem category.
- A
- 18
What technique is used in the functional version of the strategy pattern to pass the algorithm?
- A
Using partial application to pre-sort data
- B
Returning a sorting function from the context function
- C
Passing a sorting function as an argument to the context function
- D
Storing sorting functions in a list and iterating
Correct answer
CPassing a sorting function as an argument to the context function
Explanation
The functional strategy replaces the class hierarchy with a context function that takes a sorting function as a parameter, enabling algorithm exchange by passing different functions.
- A
- 19
In the functional observer pattern, what data structure holds the observer functions?
- A
A list
- B
A set
- C
A tuple
- D
A dictionary
Correct answer
AA list
Explanation
The functional observer pattern uses a list of observer functions that are passed to the notify function and called in order, as shown in the video example.
- A
- 20
What is one advantage of the functional approach over the class-based approach mentioned in the video?
- A
Faster execution speed
- B
Fewer lines of code
- C
Better inheritance hierarchy
- D
Easier debugging
Correct answer
BFewer lines of code
Explanation
The speaker notes that functional versions of the patterns often result in less code (e.g., 50 lines vs. over 70 lines for the template method), making them simpler.
- A
- 21
What is the technique called when a function returns another function, as used in the template method example?
- A
Function composition
- B
Callback registration
- C
Partial application
- D
Returning a function as a result
Correct answer
DReturning a function as a result
Explanation
The closure example demonstrates returning a function from another function, a common functional programming technique that adds flexibility by pre-applying fixed parameters.
- A
- 22
What does functools.partial allow you to do?
- A
Decorate functions with additional behavior
- B
Pre-apply some arguments to a function, creating a new function
- C
Execute a function in a separate thread
- D
Convert a class method into a standalone function
Correct answer
BPre-apply some arguments to a function, creating a new function
Explanation
partialfrom functools creates a callable with some arguments already supplied, reducing repetition when calling a function with the same fixed arguments repeatedly. - A
- 23
In the functional template method, how is the closure version different from the version using functools.partial?
- A
The closure is faster than partial
- B
There is no difference; they are identical
- C
The partial version cannot handle default hook values
- D
The closure requires defining an inner function, while partial creates a new function without nesting
Correct answer
DThe closure requires defining an inner function, while partial creates a new function without nesting
Explanation
The closure approach defines an inner function that captures the required operations, while
partialachieves the same effect by fixing arguments without explicit nesting, as shown in the video. - A

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