Behavioral Design Patterns

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

Category
Behavioral Design
Level
intermediate
Languages
en → en
Cards
45
Markdown
Behavioral Design Patterns (.md)

Flashcards (45)

#Front (Term / Question)Back (Definition / Answer)ExampleMultiple-Choice Options
1What 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.
2What 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.
3What is the role of the 'detach' method in the Observer pattern?The 'detach' method allows observers to unregister from the Subject, stopping notifications.
4What 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.
5How 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.
6What 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.
7What 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.
8What is a fallback strategy in the Strategy pattern?A fallback strategy is a default algorithm that is used when no specific strategy is provided.
9How 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.
10What 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.
11What 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.
12What is the 'setStrategy' method in the Strategy pattern?The 'setStrategy' method allows the Context to change its current Strategy object at runtime.
13What 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.
14What 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.
15What 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.
16What 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.
17What is a behavioral design pattern?A behavioral design pattern is a design pattern that focuses on how objects interact and communicate with one another.
18What 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.
19How can the State pattern simplify complex conditional logic?The State pattern eliminates complex conditional statements by encapsulating state-specific behavior in separate classes.
20How does the Observer pattern handle multiple observers?The Observer pattern allows multiple observers to register with a single subject and receive updates independently.
21What is a state object in the State pattern?A state object implements behavior associated with a particular state of the Context.
22What 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.
23What is the purpose of the 'undo' method in the Command pattern?The 'undo' method reverses the last executed command, restoring the previous state.
24What 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.
25What 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.
26What 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.
27What is a strategy interface in the Strategy pattern?A strategy interface defines the common methods that all concrete strategies must implement.
28What 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.
29What 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.
30What 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.
31Can 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.
32What is a command receiver in the Command pattern?A command receiver is the object that performs the actual work when a command is executed.
33What 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.
34What 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.
35How 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.
36What 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.
37What 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.
38What 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.
39What 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.
40What 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.
41What 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.
42How 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.
43What 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.
44What 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.
45What 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.