Design Patterns Introduction
- 31Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is the primary purpose of design patterns in software development?
What is the primary purpose of design patterns in software development?
31 questions
- 1
What is the primary purpose of design patterns in software development?
- A
To replace the need for any architectural planning in software projects
- B
To provide ready-made code snippets that can be copied directly into any project
- C
To offer standardized, reusable solutions to common problems that improve code quality and communication
- D
To enforce a specific programming language and framework for all projects
Correct answer
CTo offer standardized, reusable solutions to common problems that improve code quality and communication
Explanation
Design patterns provide standard ways to solve recurring problems, ensuring solutions are tested and improve scalability, testability, and separation of concerns.
- A
- 2
Which of the following is NOT a benefit of using design patterns as mentioned in the transcript?
- A
They guarantee that the solution is the most efficient in all situations
- B
They introduce standard terminology for easier communication among developers
- C
They enhance testability and scalability of the application
- D
They promote separation of concerns and better architecture
Correct answer
AThey guarantee that the solution is the most efficient in all situations
Explanation
Design patterns offer proven solutions but do not guarantee absolute efficiency; they are best practices, but context matters.
- A
- 3
What is the 'Gang of Four' in the context of design patterns?
- A
A set of four design patterns that are considered the most important
- B
A development team that created the first design pattern compiler
- C
A group of four programming languages that support design patterns natively
- D
Four software architects who wrote a foundational book on design patterns
Correct answer
DFour software architects who wrote a foundational book on design patterns
Explanation
The Gang of Four refers to Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, who authored the seminal book on design patterns.
- A
- 4
Which category of design patterns focuses on how objects are created?
- A
Creational patterns
- B
Architectural patterns
- C
Structural patterns
- D
Behavioral patterns
Correct answer
ACreational patterns
Explanation
Creational patterns deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.
- A
- 5
What is the main concern of structural design patterns?
- A
How to manage the lifecycle of objects in memory
- B
How classes and objects are composed to form larger structures
- C
How objects are created and instantiated
- D
How objects interact and communicate with each other
Correct answer
BHow classes and objects are composed to form larger structures
Explanation
Structural patterns explain how to assemble objects and classes into larger structures while keeping them flexible and efficient.
- A
- 6
Which design pattern type focuses on how components interact and communicate with each other?
- A
Behavioral patterns
- B
Concurrency patterns
- C
Creational patterns
- D
Structural patterns
Correct answer
ABehavioral patterns
Explanation
Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects, focusing on communication.
- A
- 7
What is a key disadvantage of the Singleton pattern mentioned in the transcript?
- A
It consumes too much memory because it creates multiple instances
- B
It cannot be used in multi-threaded applications at all
- C
It breaks the single responsibility principle and can cause testing issues
- D
It is too complex to implement in most programming languages
Correct answer
CIt breaks the single responsibility principle and can cause testing issues
Explanation
Singletons violate the single responsibility principle by managing their own lifecycle, and they make testing harder due to tight coupling.
- A
- 8
In the Singleton pattern, how does the naive implementation fail in a multi-threaded environment?
- A
It only works in Python and fails in other languages like Java
- B
It throws an exception if two threads try to access the Singleton at the same time
- C
It causes a deadlock because multiple threads wait for each other
- D
It may create multiple instances when two threads simultaneously check for existence
Correct answer
DIt may create multiple instances when two threads simultaneously check for existence
Explanation
The naive implementation without locking can lead to race conditions where two threads both pass the 'if not instance' check and create separate instances.
- A
- 9
What is the primary purpose of the Factory Method pattern?
- A
To provide a simplified interface to a complex subsystem
- B
To copy existing objects without depending on their classes
- C
To create a single instance of a class globally
- D
To separate the creation of an object from its use, allowing dynamic switching of implementations
Correct answer
DTo separate the creation of an object from its use, allowing dynamic switching of implementations
Explanation
Factory Method defines an interface for creating an object, but lets subclasses decide which class to instantiate, promoting loose coupling.
- A
- 10
How does Abstract Factory differ from Factory Method?
- A
Abstract Factory is used only for creating database connections, while Factory Method is for any object
- B
Abstract Factory creates a single object, while Factory Method creates multiple objects
- C
Abstract Factory is a factory that creates other factories, adding another level of abstraction
- D
Abstract Factory cannot be implemented in Python due to lack of interfaces
Correct answer
CAbstract Factory is a factory that creates other factories, adding another level of abstraction
Explanation
Abstract Factory provides an interface for creating families of related objects without specifying their concrete classes, essentially a factory of factories.
- A
- 11
What problem does the Builder pattern solve?
- A
It simplifies the construction of an object with many optional parameters, avoiding telescoping constructors
- B
It ensures that only one thread can create an object at a time
- C
It enables copying of complex objects without coupling to their classes
- D
It allows creating a single instance of a resource-intensive object
Correct answer
AIt simplifies the construction of an object with many optional parameters, avoiding telescoping constructors
Explanation
The Builder pattern separates the construction of a complex object from its representation, making it easier to handle multiple optional parameters.
- A
- 12
In the transcript, what is the simpler alternative to the classic Builder pattern in Python?
- A
Using a Singleton metaclass to manage instances
- B
Using named parameters with default values in the constructor
- C
Using the Prototype pattern to clone objects
- D
Using the
__new__method with default parameters
Correct answer
BUsing named parameters with default values in the constructor
Explanation
Python allows using named parameters with default values in
__init__, which can eliminate the need for a separate Builder class for many cases. - A
- 13
What is the core idea of the Prototype pattern?
- A
To define a family of interchangeable object factories
- B
To create new objects by copying an existing object, known as the prototype
- C
To restrict a class to only one instance
- D
To provide a way to build complex objects step by step
Correct answer
BTo create new objects by copying an existing object, known as the prototype
Explanation
The Prototype pattern creates new objects by copying an existing object, avoiding the cost of creating from scratch and hiding the complexity of object creation.
- A
- 14
Which Python module provides built-in support for the Prototype pattern?
- A
collectionsmodule - B
copymodule - C
picklemodule - D
abcmodule
Correct answer
BcopymoduleExplanation
Python's
copymodule providescopy()anddeepcopy()functions to implement prototype-based cloning. - A
- 15
What is the purpose of the Adapter pattern?
- A
To convert the interface of a class into another interface that clients expect
- B
To separate abstraction from implementation so they can vary independently
- C
To provide a simplified interface to a complex system
- D
To attach additional behavior to an object without changing its interface
Correct answer
ATo convert the interface of a class into another interface that clients expect
Explanation
The Adapter pattern allows incompatible interfaces to work together by converting one interface to another that the client expects.
- A
- 16
What problem does the Bridge pattern solve?
- A
It manages the lifecycle of a resource that is expensive to create
- B
It provides a way to access elements of a collection sequentially
- C
It allows an object to alter its behavior when its internal state changes
- D
It avoids class explosion when dealing with multiple orthogonal dimensions (like shape and color)
Correct answer
DIt avoids class explosion when dealing with multiple orthogonal dimensions (like shape and color)
Explanation
The Bridge pattern decouples an abstraction from its implementation so that the two can vary independently, preventing an exponential growth of classes.
- A
- 17
In the Bridge pattern example from the transcript, what example is used to illustrate the pattern?
- A
A graphic editor with shapes and colors
- B
A remote control and devices (radio and TV)
- C
A data storage system with caching
- D
A computer with CPU and memory
Correct answer
BA remote control and devices (radio and TV)
Explanation
The transcript uses a remote control (abstraction) and devices (implementation) to demonstrate how the Bridge pattern works.
- A
- 18
What is the main advantage of the Composite pattern?
- A
It provides a way to update the behavior of a class without modifying it
- B
It reduces the number of classes needed in a tree structure
- C
It allows treating individual objects and compositions of objects uniformly
- D
It simplifies the creation of complex objects by separating construction from representation
Correct answer
CIt allows treating individual objects and compositions of objects uniformly
Explanation
The Composite pattern enables clients to treat individual objects and composite objects uniformly, making it easier to work with tree structures.
- A
- 19
How does the Decorator pattern add behavior to an object?
- A
By creating a subclass that overrides all methods of the original class
- B
By wrapping the object with a new class that provides additional functionality before or after calling the original method
- C
By modifying the object's class at runtime using reflection
- D
By using a separate factory that generates extended objects
Correct answer
BBy wrapping the object with a new class that provides additional functionality before or after calling the original method
Explanation
The Decorator pattern attaches additional responsibilities to an object dynamically by placing it inside a wrapper object that contains the behavior.
- A
- 20
What is the key difference between the Proxy pattern and the Decorator pattern?
- A
Proxy is used for remote objects, while Decorator is for local objects only
- B
Proxy manages the lifecycle of the underlying object, while Decorator simply adds behavior
- C
Proxy provides a simplified interface, while Decorator provides the same interface
- D
Proxy changes the interface of an object, while Decorator keeps the same interface
Correct answer
BProxy manages the lifecycle of the underlying object, while Decorator simply adds behavior
Explanation
The Proxy pattern controls access and manages the lifecycle of the real subject, whereas the Decorator adds or overrides behavior without managing lifecycle.
- A
- 21
What does the Facade pattern provide?
- A
A method to copy objects without revealing their internal structure
- B
A mechanism to handle multiple requests in a chain
- C
A way to encapsulate a request as an object
- D
A simplified interface to a complex subsystem
Correct answer
DA simplified interface to a complex subsystem
Explanation
The Facade pattern provides a unified and simpler interface to a set of interfaces in a subsystem, making it easier to use.
- A
- 22
What is the primary goal of the Flyweight pattern?
- A
To decouple an abstraction from its implementation
- B
To provide a global point of access to a resource
- C
To reduce memory usage by sharing common parts of objects among multiple objects
- D
To allow an object to alter its behavior when its state changes
Correct answer
CTo reduce memory usage by sharing common parts of objects among multiple objects
Explanation
Flyweight minimizes memory usage by sharing as much data as possible (intrinsic state) with other similar objects.
- A
- 23
In the Flyweight example in the transcript, what is stored in the factory versus what is unique per object?
- A
The factory stores the rank (private, sergeant, major), while the position is unique per fighter
- B
The factory stores the position, while the rank is unique per fighter
- C
The factory stores nothing; each fighter has its own copy of everything
- D
The factory stores the sprite images, while the rank is unique
Correct answer
AThe factory stores the rank (private, sergeant, major), while the position is unique per fighter
Explanation
The fighter factory stores only three fighter objects based on rank (intrinsic), while each fighter created by the army has its own state (extrinsic) not shown in the code.
- A
- 24
What is the Chain of Responsibility pattern's basic structure?
- A
A tree structure where leaves and composites are treated uniformly
- B
A set of handlers arranged in a chain where each handler can process a request or pass it to the next
- C
A stack of commands that can be undone and redone
- D
A central mediator that routes messages between components
Correct answer
BA set of handlers arranged in a chain where each handler can process a request or pass it to the next
Explanation
Chain of Responsibility decouples sender and receiver by allowing multiple objects to handle a request, with each handler having a reference to the next.
- A
- 25
What does the Command pattern encapsulate?
- A
A request as an object, allowing parameterization and queuing of requests
- B
The communication between objects via a central mediator
- C
The state of an object to allow undo/redo functionality
- D
The behavior of an object based on its internal state
Correct answer
AA request as an object, allowing parameterization and queuing of requests
Explanation
The Command pattern turns a request into a stand-alone object that contains all information about the request, enabling operations like queuing, logging, and undo.
- A
- 26
In the Interpreter pattern, what are terminal and non-terminal expressions?
- A
Terminal expressions are leaf nodes (e.g., numbers), non-terminal expressions are composite nodes (e.g., operations)
- B
Terminal expressions represent grammar rules, non-terminal represent values
- C
Terminal expressions are the final results, non-terminal are intermediate states
- D
Terminal expressions cannot be changed, non-terminal can be overridden
Correct answer
ATerminal expressions are leaf nodes (e.g., numbers), non-terminal expressions are composite nodes (e.g., operations)
Explanation
In the Interpreter pattern, terminal expressions define the basic elements (like numbers), and non-terminal expressions define the composition rules (like operations).
- A
- 27
What are the two core methods of an Iterator?
- A
execute()andundo() - B
add()andremove() - C
next()andhasNext() - D
push()andpop()
Correct answer
Cnext()andhasNext()Explanation
The Iterator pattern typically provides
hasNext()to check if more elements exist andnext()to retrieve the next element. - A
- 28
What is the role of the Mediator pattern?
- A
To provide a way to save and restore previous states of an object
- B
To centralize communication between objects, reducing direct dependencies
- C
To define a one-to-many dependency between objects so that when one object changes state, all dependents are notified
- D
To encapsulate a request as an object
Correct answer
BTo centralize communication between objects, reducing direct dependencies
Explanation
The Mediator pattern defines an object that encapsulates how a set of objects interact, promoting loose coupling by preventing direct references.
- A
- 29
In the Memento pattern, which component is responsible for storing and restoring the state?
- A
Caretaker
- B
Originator
- C
Memento
- D
Observer
Correct answer
ACaretaker
Explanation
The Caretaker manages the saved states (mementos) and decides when to save and restore, while the Originator creates and restores state using Memento objects.
- A
- 30
What is the main advantage of the Observer pattern over polling?
- A
It eliminates the need for multiple subscribers
- B
It allows synchronous execution of event handlers
- C
It reduces unnecessary communication by notifying only interested parties when an event occurs
- D
It guarantees that events are processed in order
Correct answer
CIt reduces unnecessary communication by notifying only interested parties when an event occurs
Explanation
Observer pattern uses a subscribe/notify mechanism where subjects push events to subscribers, avoiding wasteful polling.
- A
- 31
What is a state machine in the context of the State pattern?
- A
A data structure for storing the history of changes
- B
A mechanism to measure the performance of an application
- C
A way to iterate over a collection of objects
- D
An object that can only be in a finite number of predefined states, and its behavior changes based on the current state
Correct answer
DAn object that can only be in a finite number of predefined states, and its behavior changes based on the current state
Explanation
The State pattern allows an object to alter its behavior when its internal state changes, appearing as if it changed its class.
- A

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