Behavioral Design Patterns Flashcards

1
Q

Provide definition for Chain of Responsibility

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is not standard approach of Chain Of Responsibility design pattern

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Request has to be sent to first handler in the chain (Chain of Responsibility pattern) or not?

A

Request can be sent to any handler in the chain—it doesn’t
have to be the first one

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to modify the order of handlers in CoR pattern in runtime

A

If you plan to modify chains at runtime,
you need to define a setter for altering the value of the reference field

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Chain of Responsibility is often used in conjunction with Composite. How Composite pattern can be adjusted for that

A

Component need to have a link to composite. This link play a role of next handler. (p.259)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How CoR is connected to the Command pattern

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Provide a definition of command pattern

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is Separation Of Concerns principle

A

Design principle for separating a computer program into distinct sections (layers)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Provide a definition of Iterator pattern

A

Behavioral design pattern that lets you traverse
elements of a collection without exposing its underlying
representation (list, stack, tree, etc.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Provide definition for Mediator design pattern

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What kind of object can Component pass to Mediator

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What else can do Mediator instead of keeping references to all components

A

They manage sometimes their lifecycle

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Provide definition for Memento design pattern

A

Behavioral design pattern that lets you save and
restore the previous state of an object without revealing the
details of its implementation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the roles of orginator and caretaker in Memento pattern

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is memento object

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

In the absence of nested classes, you can restrict access to
the memento’s fields

A

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

17
Q

How originators can work with a memento in the model of implementation based on an intermediate interface?

A

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.

18
Q

In which way you can avoid that bussiness logic is mixed together with subscribction methods in Observer Pattern

A

You can create something like EventManager which will implement only subscription methods. And then create bussiness logic class which will composite EventManager

19
Q

Structure of Strategy and State patterns look similar. What is a key difference?

A

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.

20
Q

How to avoid duplication of similar code across multiple states.

A

You may provide intermediate abstract classes that encapsulate some common behaviour

21
Q

How an optional step with empty body is called in template method pattern?

A

Hook (usually placed before and after crucial algorithm step.)

22
Q

What kind of steps you can distinguish in template method pattern

A

Abstract, optional, hook.

23
Q

Why Template Method pattern may violate Liskov Substitution Princliple?

A

The rule may be violated by suppressing a default step implementation via a subclass

24
Q

Provide a definition for Visitor pattern

A

Behavioral design pattern that lets you separate algorithms from the objects on which they operate

25
Q

What should you do when encounter a situation where the visitor will need access to some private members of the element class

A

In this case, you can either make these fields or methods public, violating the element’s encapsulation, or nest the visitor class in the element class.

26
Q

What means late or dynamic binding?

A

Checking class of the object passed as parameter and picking the method implementation from the appropriate class.

27
Q

Why we use the word late in binding?

A

Because we link object and its implementation after compilation, at runtime.

28
Q

Why we use the word dynamic in binding?

A

Because every new object might need to be linked to a different implementation.

29
Q

What means early or static binding?

A

Take place in compile time and compiler. When there are overloaded methods compiler checks its exact type. Inheritance hierarhy si nto taken into account.

30
Q

Why we use the word early in early binding?

A

Because it happens at compile time, before the program is launched.

31
Q

Why we use the word static in static binding?

A

Because it can’t be altered at runtime

32
Q

What is double dispatch?

A

Double dispatch is a trick that allows using dynamic binding alongside with overloaded methods (overloaded methods are not dynamically binded).

33
Q

Why we need an accept methods in Visitor pattern?

A

In order to use Double Dispatch trick (allow dynamic binding for overloaded methods)