Behavioral Flashcards

1
Q

What is a “Chain of Responsibility”?

A

The sender’s request goes to a chain of objects and can be handled by any object in the chain.

The pattern avoids coupling the sender of a request to a receiver and gives the request multiple chances to be handled.

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

What is a Command Pattern?

A

A command pattern encapsulates a request under an object as a command and passes it to an invoker object. The invoker finds the appropriate object to hand the command, passes it along to the corresponding object.

The pattern has clear separation of concerns, and it is easy to add new commands.

It is good for when you need to create and execute requests at different times.

Also useful for logging, rollback, and transactions.

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

What is an interpreter pattern?

A

An interpreter pattern defines a representation of grammar of a given language, along with an interpreter that uses this representation to interpret sentences in the language.

Useful for when efficiency is not a priority, it is easier to change and extend grammar.

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

What is an iterator pattern?

A

A pattern that is used to access elements of a collection, without exposing the underlying implementation.

It will support variations in the traversal of a collection. And it simplifies the interface of the collection.

Uses a method to get the “next” element in the collection. And, signals when the collection is done.

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

What is a mediator pattern?

A

A mediator pattern is an object that encapsulates how a set of objects interact. It simplifies object protocols.

And centralizes control.

The pattern is common in message and chat based applications

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

What is a Memento Pattern

A

AKA token

The memento pattern restores the state of an object to its previous state. But it must do this without violating Encapsulation.

Undo, redo, backspace are examples of the memento pattern.

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

What is an observer pattern?

A

An observer pattern defines a one-to-one dependency so that when one object changes state, all its dependents are notified and updated automatically.

Just like a subscribe button

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

What is a state pattern?

A

A state pattern changes behavior based on state.

Various objects are created that represent state and a context object that varies as its state object changes.

It makes state transitions explicit and keeps state-specific behavior.

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

What is a strategy pattern?

A

A strategy pattern defies a family of functionality, encapsulates each one, and makes them interchangeable.

It provides a substitute to subclassing.

It defines each behavior within its own class, eliminating the need for conditional statements.

It makes it easier to extend and incorporate new behavior without changing the application.

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

What is a template pattern?

A

A template pattern defines the skeleton of a function in operation, deferring some steps to sub-classes.

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

What are the behavioral design patterns?

A

Chain of Responsibility Pattern
Command Pattern
Interpreter Pattern
Iterator Pattern
Mediator Pattern
Memento Pattern
Observer Pattern
State Pattern
Strategy Pattern
Template Pattern
Visitor Pattern
Null Object

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

Why is a Template Pattern a bad idea in modern programming languages?

A

Template Pattern uses subclasses and method overriding. It’s much easier to just pass in a higher order function as a parameter.

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

Why aren’t strategy patterns necessary in languages with higher order functions?

A

You can just pass in the function you need.

The strategy pattern is fundamentally avoided, in Java a super interface is preferable for loose coupling.

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

What two behavioral patterns are bad ideas?

A

Template Pattern and Strategy Pattern

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

What two behavioral patterns are so popular they’re being built as part of languages now?

A

Visitor and Iterator pattern. We seen them introduced as iterators or generators.

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

How are the Visitor and Iterator pattern similar?

A

Iterator Pattern:
Producer offers a callback method next().
Consumer loops calling next() over and over.

Visitor Pattern:
Producer iterates across a data structure.
Consumer offers one or more callback methods.

17
Q

Why is the observer pattern a bad idea?

A

First of all it tightly couples everything.

Second of all it is like building a massive forest that subscribes all the trees to each other’s changes. Which makes the data flow very complex.

Lastly it scatters data rather than unifying it.

18
Q

Why is the Memento Pattern a bad idea?

A

1) the Command Pattern covers the same responsibility.

2) It hides how data is persisted to disk.

3) the Memento Patterns requires state saved as an object string. When it is much better to use standard tools such as SQL or Postgres.