Behavioral Flashcards
What is a “Chain of Responsibility”?
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.
What is a Command Pattern?
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.
What is an interpreter pattern?
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.
What is an iterator pattern?
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.
What is a mediator pattern?
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
What is a Memento Pattern
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.
What is an observer pattern?
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
What is a state pattern?
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.
What is a strategy pattern?
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.
What is a template pattern?
A template pattern defines the skeleton of a function in operation, deferring some steps to sub-classes.
What are the behavioral design patterns?
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
Why is a Template Pattern a bad idea in modern programming languages?
Template Pattern uses subclasses and method overriding. It’s much easier to just pass in a higher order function as a parameter.
Why aren’t strategy patterns necessary in languages with higher order functions?
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.
What two behavioral patterns are bad ideas?
Template Pattern and Strategy Pattern
What two behavioral patterns are so popular they’re being built as part of languages now?
Visitor and Iterator pattern. We seen them introduced as iterators or generators.