Behavioral Design Patterns Flashcards
Provide definition for Chain of Responsibility
Behavioral design pattern that lets
you pass requests along a chain of handlers. Upon receiving a
request, each handler decides either to process the request or
to pass it to the next handler in the chain
What is not standard approach of Chain Of Responsibility design pattern
Upon receiving a request, a handler decides whether it can process it. If it can, it doesn’t pass
the request any further. So it’s either only one handler that
processes the request or none at all.
Request has to be sent to first handler in the chain (Chain of Responsibility pattern) or not?
Request can be sent to any handler in the chain—it doesn’t
have to be the first one
How to modify the order of handlers in CoR pattern in runtime
If you plan to modify chains at runtime,
you need to define a setter for altering the value of the reference field
Chain of Responsibility is often used in conjunction with Composite. How Composite pattern can be adjusted for that
Component need to have a link to composite. This link play a role of next handler. (p.259)
How CoR is connected to the Command pattern
Handlers in Chain of Responsibility can be implemented as
Commands. In this case, you can execute a lot of different
operations over the same context object, represented by a
request.
However, there’s another approach, where the request itself
is a Command object. In this case, you can execute the same
operation in a series of different contexts linked into a chain
Provide a definition of command pattern
Command is a behavioral design pattern that turns a request into a stand-alone object that contains all information about the request. This transformation lets you parameterize
methods with different requests, delay or queue a request’s execution, and support undoable operations
What is Separation Of Concerns principle
Design principle for separating a computer program into distinct sections (layers)
Provide a definition of Iterator pattern
Behavioral design pattern that lets you traverse
elements of a collection without exposing its underlying
representation (list, stack, tree, etc.)
Provide definition for Mediator design pattern
Behavioral design pattern that lets you reduce chaotic dependencies between objects. The pattern restricts direct communications between the objects and forces them to collaborate only via a mediator object
What kind of object can Component pass to Mediator
Components may pass any context as arguments of this method, including their own objects, but only in such a way that no coupling occurs between a receiving component and the sender’s class.
What else can do Mediator instead of keeping references to all components
They manage sometimes their lifecycle
Provide definition for Memento design pattern
Behavioral design pattern that lets you save and
restore the previous state of an object without revealing the
details of its implementation
What are the roles of orginator and caretaker in Memento pattern
Caretaker is an object which makes snapshot and produces memento.
Caretaker is an oblect which can use memento but only via the limited interface, it’s not able to
tamper with the state stored inside the memento.
What is memento object
Value object that acts as a snapshot of the
originator’s state. It’s a common practice to make the memento
immutable and pass it the data only once, via the constructor
In the absence of nested classes, you can restrict access to
the memento’s fields
By establishing a convention that caretakers can work with a memento only through an explicit-
ly declared intermediary interface, which would only declare
methods related to the memento’s metadata
How originators can work with a memento in the model of implementation based on an intermediate interface?
Originators can work with a memento
object directly, accessing fields and methods declared in the
memento class. The downside of this approach is that you
need to declare all members of the memento public.
In which way you can avoid that bussiness logic is mixed together with subscribction methods in Observer Pattern
You can create something like EventManager which will implement only subscription methods. And then create bussiness logic class which will composite EventManager
Structure of Strategy and State patterns look similar. What is a key difference?
In the state pattern the particular states may na aware of each other and initiate transitions from one state to another, whereas strategies almost never know each other.
How to avoid duplication of similar code across multiple states.
You may provide intermediate abstract classes that encapsulate some common behaviour
How an optional step with empty body is called in template method pattern?
Hook (usually placed before and after crucial algorithm step.)
What kind of steps you can distinguish in template method pattern
Abstract, optional, hook.
Why Template Method pattern may violate Liskov Substitution Princliple?
The rule may be violated by suppressing a default step implementation via a subclass
Provide a definition for Visitor pattern
Behavioral design pattern that lets you separate algorithms from the objects on which they operate