# Behavioral Design Patterns

> Core patterns for managing object interactions and responsibilities, including Observer, Strategy, Command, and State patterns

- **Canonical URL:** https://flashcards.world/flashcards/sets/dd2d4542-cfe1-4906-b02d-032ac686f9fe/
- **Markdown URL:** https://flashcards.world/flashcards/sets/dd2d4542-cfe1-4906-b02d-032ac686f9fe.md
- **Category:** Behavioral Design
- **Level:** intermediate
- **Languages:** en → en
- **Total Cards:** 45

## Flashcards (45 Cards)

| # | Front (Term / Prompt) | Back (Definition / Answer) | Example | Multiple-Choice Options |
| :--- | :--- | :--- | :--- | :--- |
| 1 | What is a concrete command in the Command pattern? | A concrete command is an implementation of the Command interface that defines the action to be performed. |  |  |
| 2 | What is the significance of the 'update' method in the Observer pattern? | The 'update' method is called by the Subject to notify observers of changes, allowing them to react accordingly. |  |  |
| 3 | What is the role of the 'detach' method in the Observer pattern? | The 'detach' method allows observers to unregister from the Subject, stopping notifications. |  |  |
| 4 | What is the purpose of the 'setState' method in the State pattern? | The 'setState' method changes the current state of the Context to a new State object. |  |  |
| 5 | How does the Context class function in the State pattern? | The Context class maintains a reference to a State object and delegates state-specific behavior to that object. |  |  |
| 6 | What is a state machine in the context of the State pattern? | A state machine is a model that describes the states and transitions of an object, often implemented using the State pattern. |  |  |
| 7 | What is a state transition table in the State pattern? | A state transition table is a visual representation of states and transitions, showing how the Context moves between states. |  |  |
| 8 | What is a fallback strategy in the Strategy pattern? | A fallback strategy is a default algorithm that is used when no specific strategy is provided. |  |  |
| 9 | How does the Context class in the Strategy pattern interact with strategies? | The Context class holds a reference to a Strategy object and delegates the execution of the algorithm to that Strategy. |  |  |
| 10 | What is the role of the Observer interface? | The Observer interface defines the method(s) that concrete observers must implement to receive updates from the Subject. |  |  |
| 11 | What is a command object in the Command pattern? | A command object encapsulates all the information needed to perform an action or trigger an event at a later time. |  |  |
| 12 | What is the 'setStrategy' method in the Strategy pattern? | The 'setStrategy' method allows the Context to change its current Strategy object at runtime. |  |  |
| 13 | What is a macro command in the Command pattern? | A macro command is a command that encapsulates multiple commands, allowing them to be executed as a single action. |  |  |
| 14 | What is the role of the 'notifyObservers' method in the Observer pattern? | The 'notifyObservers' method is called by the Subject to inform all registered observers of a state change. |  |  |
| 15 | What is the difference between the Observer and Mediator patterns? | The Observer pattern promotes a one-to-many relationship between objects, while the Mediator pattern centralizes communication between objects to reduce direct dependencies. |  |  |
| 16 | What is a concrete observer in the Observer pattern? | A concrete observer is an implementation of the Observer interface that defines how to respond to notifications from the Subject. |  |  |
| 17 | What is a behavioral design pattern? | A behavioral design pattern is a design pattern that focuses on how objects interact and communicate with one another. |  |  |
| 18 | What is a 'listener' in the context of the Observer pattern? | A listener is an object that registers to receive notifications from a Subject when its state changes. |  |  |
| 19 | How can the State pattern simplify complex conditional logic? | The State pattern eliminates complex conditional statements by encapsulating state-specific behavior in separate classes. |  |  |
| 20 | How does the Observer pattern handle multiple observers? | The Observer pattern allows multiple observers to register with a single subject and receive updates independently. |  |  |
| 21 | What is a state object in the State pattern? | A state object implements behavior associated with a particular state of the Context. |  |  |
| 22 | What is the purpose of the 'redo' method in the Command pattern? | The 'redo' method re-executes a command that has been undone, restoring the state again. |  |  |
| 23 | What is the purpose of the 'undo' method in the Command pattern? | The 'undo' method reverses the last executed command, restoring the previous state. |  |  |
| 24 | What is the purpose of the 'execute' method in the Command pattern? | The 'execute' method is called to perform the action encapsulated by the command object. |  |  |
| 25 | What is a queue of commands in the Command pattern? | A queue of commands is a collection of command objects that can be executed in sequence or stored for later execution. |  |  |
| 26 | What is the role of the 'context' in the Command pattern? | The context in the Command pattern is the object that invokes the command, often managing the command's execution and history. |  |  |
| 27 | What is a strategy interface in the Strategy pattern? | A strategy interface defines the common methods that all concrete strategies must implement. |  |  |
| 28 | What is the difference between context and strategy in the Strategy pattern? | The context is the object that uses a strategy, while the strategy is the algorithm that the context can use. |  |  |
| 29 | What is a state transition in the State pattern? | A state transition occurs when the Context changes its current State object to another State object, altering its behavior. |  |  |
| 30 | What is the Observer pattern? | The Observer pattern defines a one-to-many dependency between objects, so when one object changes state, all its dependents are notified and updated automatically. |  |  |
| 31 | Can you explain the concept of 'encapsulation' in behavioral patterns? | Encapsulation in behavioral patterns refers to the practice of bundling the data (state) and methods (behavior) that operate on the data into a single unit or class. |  |  |
| 32 | What is a command receiver in the Command pattern? | A command receiver is the object that performs the actual work when a command is executed. |  |  |
| 33 | What is the purpose of the Invoker in the Command pattern? | The Invoker is responsible for initiating commands and can keep track of command history for undo functionality. |  |  |
| 34 | What is a transition method in the State pattern? | A transition method is a method that changes the state of the Context, typically by invoking a method on the current State object. |  |  |
| 35 | How does the Observer pattern promote loose coupling? | The Observer pattern allows subjects and observers to interact without needing to know about each other's implementations, promoting loose coupling. |  |  |
| 36 | What are the benefits of using the Strategy pattern? | The Strategy pattern promotes code reusability, flexibility, and the ability to switch algorithms at runtime without altering the client code. |  |  |
| 37 | What is a state-specific behavior in the State pattern? | State-specific behavior is the behavior defined by a particular state, which is executed when the Context is in that state. |  |  |
| 38 | What is the main purpose of the Strategy pattern? | The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable, allowing the algorithm to vary independently from clients that use it. |  |  |
| 39 | What is a concrete strategy in the Strategy pattern? | A concrete strategy is a specific implementation of an algorithm defined in the Strategy pattern that can be selected and executed by the context. |  |  |
| 40 | What role does the Subject play in the Observer pattern? | In the Observer pattern, the Subject maintains a list of observers and notifies them of any state changes. |  |  |
| 41 | What is the State pattern used for? | The State pattern allows an object to alter its behavior when its internal state changes, appearing to change its class. |  |  |
| 42 | How does the Command pattern work? | The Command pattern encapsulates a request as an object, thereby allowing for parameterization of clients with queues, requests, and operations. |  |  |
| 43 | What is the significance of the 'currentState' variable in the State pattern? | The 'currentState' variable holds a reference to the current State object, determining the behavior of the Context. |  |  |
| 44 | What is a context in the Strategy pattern? | A context is the object that uses a Strategy to perform a task, delegating the specific algorithm to the Strategy. |  |  |
| 45 | What is the role of the 'addObserver' method in the Observer pattern? | The 'addObserver' method registers a new observer with the Subject, allowing it to receive notifications. |  |  |

---
Study this deck interactively with FSRS-6 spaced repetition, audio review, and multiple-choice mode at [Flashcards World](https://flashcards.world/flashcards/sets/dd2d4542-cfe1-4906-b02d-032ac686f9fe/).
