Design Patterns Introduction
- 30Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What are design patterns?
30 questions
- 1
What are design patterns?
Model answer
Design patterns are standard, accepted ways of solving similar problems in software development that provide solutions with benefits like scalability, testability, and separation of concerns.
Explanation
Design patterns are reusable solutions to common problems, enabling developers to use proven approaches and communicate effectively using standard terminology.
- 2
List three benefits of using design patterns.
Model answer
Scalability, testability, and separation of concerns (or standard terminology and best practices).
Explanation
Design patterns help code grow (scalability), make testing easier (testability), and ensure components are responsible for their own tasks (separation of concerns).
- 3
Who are the Gang of Four?
Model answer
The Gang of Four is a group of four software architects who wrote the foundational book on design patterns.
Explanation
Their book is the basis for many design pattern courses, describing best practices for solving common problems.
- 4
What are the three main types of design patterns?
Model answer
Creational, structural, and behavioral.
Explanation
Creational patterns deal with object creation, structural with how components are structured, and behavioral with how components interact.
- 5
What problem does the Singleton pattern solve?
Model answer
It ensures only one instance of a class is created, providing a single point of access to a shared resource like a network communicator or database.
Explanation
This avoids resource inefficiency and allows central management of requests.
- 6
Why is Singleton often criticized?
Model answer
It breaks the single responsibility principle, reduces testability due to tight coupling, and holds state for the entire application lifetime.
Explanation
The Singleton manages its own creation and is accessed by multiple components, making mocking difficult and introducing global state.
- 7
How does a naive Singleton fail in a multi-threaded environment?
Model answer
Two threads can simultaneously check the instance condition and create separate instances, breaking the singleton guarantee.
Explanation
The naive implementation lacks synchronization, so concurrent creation leads to multiple instances.
- 8
How do you make a Singleton thread-safe?
Model answer
Use a lock to synchronize the creation code, ensuring only one thread creates the instance.
Explanation
By locking the critical section, other threads wait until the instance is created.
- 9
What problem does the Factory Method pattern solve?
Model answer
It separates object creation from the client, allowing dynamic switching between different implementations without tight coupling.
Explanation
Changes in requirements (e.g., different data sources) can be handled by adding new factories without altering client code.
- 10
How does Abstract Factory differ from Factory Method?
Model answer
Abstract Factory is one level higher: it creates factories (families of related objects), while Factory Method creates individual objects.
Explanation
Abstract Factory provides an interface for creating families of products, further abstracting the creation process.
- 11
What is the purpose of the Builder pattern?
Model answer
It allows constructing complex objects with many optional parameters in a flexible and readable way, avoiding multiple constructors.
Explanation
Builders stepwise construct objects, and in Python, named parameters with defaults offer a simpler alternative.
- 12
What Python feature can simplify the Builder pattern?
Model answer
Named parameters with default values in the
__init__method, allowing optional parameters to be set by name.Explanation
This avoids the need for a separate builder class for simple cases.
- 13
What problem does the Prototype pattern solve?
Model answer
It allows copying existing objects without depending on their concrete classes, useful for complex objects with existing state.
Explanation
The object itself provides the copy functionality, avoiding tight coupling and field-by-field copying.
- 14
What is the difference between
copy.copyandcopy.deepcopyin Python?Model answer
copy.copycreates a shallow copy (references to objects), whilecopy.deepcopycreates a deep copy (copies objects themselves).Explanation
Deep copy recursively copies all nested objects, ensuring complete independence.
- 15
What is the Adapter pattern used for?
Model answer
It converts one interface into another that the client expects, allowing incompatible classes to work together.
Explanation
It is often used to integrate third-party libraries without modifying their code.
- 16
How does the Adapter pattern avoid modifying existing code?
Model answer
By placing an adapter class between the client and the adaptee, the adapter translates calls without altering the adaptee.
Explanation
The adapter inherits from both the target interface and the adaptee, or uses composition to delegate calls.
- 17
What problem does the Bridge pattern solve?
Model answer
It avoids exponential growth of classes due to multiple inheritance by separating orthogonal dimensions into separate class hierarchies and composing them.
Explanation
Instead of having a class for each combination (e.g., RedCircle, BlueCircle), Bridge separates shape and color into independent hierarchies connected via a bridge reference.
- 18
When is the Composite pattern useful?
Model answer
When you have a tree structure of objects and want to treat individual objects and composites uniformly, e.g., applying a price calculation to a computer's components.
Explanation
Composite allows you to work with simple and complex elements using the same interface.
- 19
What is the Decorator pattern?
Model answer
It attaches new behavior to an object dynamically without altering its code, by wrapping it in a decorator class.
Explanation
Decorators provide a flexible alternative to subclassing for extending functionality.
- 20
How does the Facade pattern simplify client code?
Model answer
It provides a simple, unified interface to a complex subsystem, hiding its complexity from the client.
Explanation
The client only interacts with the facade, which handles the underlying complex interactions.
- 21
What is the goal of the Flyweight pattern?
Model answer
To reduce memory and processing overhead by sharing common state among many objects, storing unique state externally.
Explanation
A factory creates and caches shared objects (e.g., sprites), so only one instance per type is stored.
- 22
How does a Proxy pattern differ from a Facade?
Model answer
The Proxy has the same interface as the underlying object and manages its lifecycle, while the Facade provides a simplified interface to a complex system.
Explanation
Proxy clients may not know they are using a proxy; facade clients know they are using a simpler interface.
- 23
What is the Chain of Responsibility pattern?
Model answer
It defines a chain of handlers, each of which can process a request or pass it to the next handler, allowing flexible request processing.
Explanation
Handlers contain a reference to the next handler, and each decides whether to consume or forward the request.
- 24
What problem does the Command pattern solve?
Model answer
It decouples the sender of a request from the receiver by encapsulating the request as an object, enabling queuing, logging, and undoable operations.
Explanation
Commands standardize how requests are packaged, reducing tight coupling between GUI and business logic.
- 25
In the Interpreter pattern, what are terminal and non-terminal expressions?
Model answer
Terminal expressions represent the basic elements (e.g., numbers), while non-terminal expressions represent grammar rules (e.g., operators) that combine terminal expressions.
Explanation
The pattern builds an abstract syntax tree and recursively evaluates it.
- 26
What are the two main methods of an Iterator?
Model answer
has_next()returns whether there are more elements, andnext()returns the next element and advances the position.Explanation
Iterators allow traversing collections without exposing their internal structure.
- 27
What is the Mediator pattern?
Model answer
It provides a central object that handles communication between multiple objects, reducing their direct dependencies.
Explanation
Objects communicate only with the mediator, which forwards messages to relevant recipients.
- 28
What are the three components of the Memento pattern?
Model answer
Originator (creates and restores mementos), Memento (stores state snapshot), and Caretaker (saves and restores mementos).
Explanation
The caretaker manages the history of mementos to enable undo/redo functionality.
- 29
What problem does the Observer pattern solve?
Model answer
It allows multiple objects (observers) to be notified when a subject's state changes, eliminating the need for polling.
Explanation
Observers subscribe to events they are interested in, and the subject notifies them automatically.
- 30
What is the core idea of the State pattern?
Model answer
An object changes its behavior based on its internal state, which is encapsulated in a state object; the program can only be in a finite number of predefined states.
Explanation
State transitions follow defined rules, and the state object is passed around to update behavior.

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