1. Home
  2. Explore
  3. Computer Science
  4. Design Patterns in Python

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Design Patterns in Python

  • 23Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Strategy Pattern

What is the primary purpose of the strategy design pattern?

23 questions

  1. 1

    What is the primary purpose of the strategy design pattern?

    1. A

      To define a standard algorithm with replaceable parts

    2. B

      To allow replacement of one algorithm with another without the system knowing

    3. C

      To notify multiple objects of changes in another object

    4. D

      To decouple an abstraction from its implementation

    Correct answer
    B

    To 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.

  2. 2

    In the class-based strategy pattern example, what method does the abstract base class define?

    1. A

      A method that attaches observers to the subject

    2. B

      A method that returns a new function from a closure

    3. C

      A method that multiplies list elements by a random number

    4. D

      A method that takes a list of integers and returns a sorted list

    Correct answer
    D

    A 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.

  3. 3

    What technique is used in the functional version of the strategy pattern?

    1. A

      Passing a function as an argument to another function

    2. B

      Using inheritance to override a base class method

    3. C

      Returning a function from a function

    4. D

      Using functools.partial to pre-apply arguments

    Correct answer
    A

    Passing 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.

  4. 4

    How does the context class in the class-based strategy pattern use the strategy?

    1. A

      It creates a new strategy instance each time it sorts

    2. B

      It inherits from the strategy class

    3. C

      It stores a strategy object and calls its sorting method

    4. D

      It directly calls a global sorting function

    Correct answer
    C

    It 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.

  5. 5

    Which of the following best describes the observer pattern?

    1. A

      It encapsulates a request as an object, allowing parameterization of clients with different requests

    2. B

      It allows an object to alter its behavior when its internal state changes

    3. C

      It provides a way to access the elements of an aggregate object sequentially

    4. D

      It defines a one-to-many dependency so that when one object changes state, all its dependents are notified

    Correct answer
    D

    It 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.

  6. 6

    In the observer pattern, what method do observers implement to receive updates?

    1. A

      attach

    2. B

      detach

    3. C

      notify

    4. D

      update

    Correct answer
    D

    update

    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.

  7. 7

    How does the subject in the observer pattern maintain decoupling from concrete observers?

    1. A

      It only knows that observers have an update method

    2. B

      It directly instantiates each observer by its class name

    3. C

      It stores observers in a dictionary mapping event types to classes

    4. D

      It uses inheritance to connect observers to the subject

    Correct answer
    A

    It 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.

  8. 8

    In the functional version of the observer pattern, what is passed to the notify function?

    1. A

      A class inheriting from Observer

    2. B

      A dictionary mapping event types to functions

    3. C

      A list of observer functions

    4. D

      A single observer function

    Correct answer
    C

    A 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.

  9. 9

    Which of the following is a typical use case for the observer pattern?

    1. A

      Sending email or UI notifications when an order is processed

    2. B

      Defining a standard algorithm where subclasses override specific steps

    3. C

      Replacing one sorting algorithm with another at runtime

    4. D

      Creating a single instance of a class shared across the system

    Correct answer
    A

    Sending 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.

  10. 10

    What is the template method pattern used for?

    1. A

      To encapsulate a request as an object, allowing parameterization of clients

    2. B

      To provide a way to access elements of a collection without exposing its representation

    3. C

      To allow an object to alter its behavior when its internal state changes

    4. D

      To define the skeleton of an algorithm, allowing subclasses to redefine certain steps

    Correct answer
    D

    To 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.

  11. 11

    In the class-based template method, what are hooks?

    1. A

      Methods that return a function for functional programming

    2. B

      Methods with default implementations that can be optionally overridden

    3. C

      Abstract methods that have no implementation

    4. D

      Methods that must be implemented by subclasses

    Correct answer
    B

    Methods 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.

  12. 12

    In the functional version of the template method, how are optional hooks handled?

    1. A

      They must always be provided as required arguments

    2. B

      They are given default values in the function signature

    3. C

      They are ignored if not provided

    4. D

      They are implemented in a separate class

    Correct answer
    B

    They 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.

  13. 13

    What technique is demonstrated by creating a function that returns another function in the template method example?

    1. A

      Using lambda functions

    2. B

      Passing a function as an argument

    3. C

      Returning a function as a result

    4. D

      Using functools.partial

    Correct answer
    C

    Returning a function as a result

    Explanation

    The closure example creates a base_template_method that returns a template_method function, illustrating how returning functions can add flexibility by pre-applying fixed operations.

  14. 14

    What does functools.partial do in the template method example?

    1. A

      It constructs a new function with some arguments pre-applied

    2. B

      It creates a closure that captures environment variables

    3. C

      It converts a class method into a standalone function

    4. D

      It returns a new function with all arguments pre-applied

    Correct answer
    A

    It constructs a new function with some arguments pre-applied

    Explanation

    partial creates a new function by fixing some arguments of an existing function, allowing the template method to be called without repeating the operations argument.

  15. 15

    In the class-based template method, what is the effect of overriding hook1 to return False?

    1. A

      Base operation three is not executed

    2. B

      Base operation three is executed twice

    3. C

      The template method stops early

    4. D

      An error is raised

    Correct answer
    A

    Base 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.

  16. 16

    According to the speaker, why are design patterns not obsolete in Python?

    1. A

      Because Python does not support first-class functions

    2. B

      Because they represent problem categories that developers need to recognize

    3. C

      Because Python cannot implement design patterns without classes

    4. D

      Because the Gang of Four book is still the only valid reference

    Correct answer
    B

    Because 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.

  17. 17

    What is the core idea of design patterns according to the speaker?

    1. A

      They are about implementing code exactly as described in the GoF book

    2. B

      They are about using inheritance and interfaces exclusively

    3. C

      They are about reducing the number of lines of code

    4. D

      They are about detecting problem classes and handling them in one's preferred way

    Correct answer
    D

    They 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.

  18. 18

    What technique is used in the functional version of the strategy pattern to pass the algorithm?

    1. A

      Using partial application to pre-sort data

    2. B

      Returning a sorting function from the context function

    3. C

      Passing a sorting function as an argument to the context function

    4. D

      Storing sorting functions in a list and iterating

    Correct answer
    C

    Passing 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.

  19. 19

    In the functional observer pattern, what data structure holds the observer functions?

    1. A

      A list

    2. B

      A set

    3. C

      A tuple

    4. D

      A dictionary

    Correct answer
    A

    A 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.

  20. 20

    What is one advantage of the functional approach over the class-based approach mentioned in the video?

    1. A

      Faster execution speed

    2. B

      Fewer lines of code

    3. C

      Better inheritance hierarchy

    4. D

      Easier debugging

    Correct answer
    B

    Fewer 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.

  21. 21

    What is the technique called when a function returns another function, as used in the template method example?

    1. A

      Function composition

    2. B

      Callback registration

    3. C

      Partial application

    4. D

      Returning a function as a result

    Correct answer
    D

    Returning 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.

  22. 22

    What does functools.partial allow you to do?

    1. A

      Decorate functions with additional behavior

    2. B

      Pre-apply some arguments to a function, creating a new function

    3. C

      Execute a function in a separate thread

    4. D

      Convert a class method into a standalone function

    Correct answer
    B

    Pre-apply some arguments to a function, creating a new function

    Explanation

    partial from functools creates a callable with some arguments already supplied, reducing repetition when calling a function with the same fixed arguments repeatedly.

  23. 23

    In the functional template method, how is the closure version different from the version using functools.partial?

    1. A

      The closure is faster than partial

    2. B

      There is no difference; they are identical

    3. C

      The partial version cannot handle default hook values

    4. D

      The closure requires defining an inner function, while partial creates a new function without nesting

    Correct answer
    D

    The 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 partial achieves the same effect by fixing arguments without explicit nesting, as shown in the video.

Question 1 of 23
Question 1 of 23

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent