Behavioral Patterns Flashcards

1
Q

Gives more than one object an opportunity to handle a request by linking receiving objects together. Use When:

  1. Multiple objects may handle a request and the handler doesn’t have to be a specific object.
  2. A set of objects should be able to handle a request with the handler determined at runtime.
  3. A request not being handled is an acceptable potential outcome.
A

CHAIN OF RESPONSIBILITY

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

Encapsulates a request allowing it to be treated as an object. This allows the request to be handled in traditionally object based relationships such as queuing and callbacks.

Use When:

  1. You need callback functionality
  2. Requests need to be handled at variant times or in variant orders.
  3. A history of requests is needed. n The invoker should be decoupled from the object handling the invocation.
A

COMMAND

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

Defines a representation for a grammar as well as a mechanism to understand and act upon the grammar.

Use When

  1. There is grammar to interpret that can be represented as large syntax trees.
  2. The grammar is simple.
  3. Efficiency is not important.
  4. Decoupling grammar from underlying expressions is desired.
A

INTERPRETER

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

Allows loose coupling by encapsulating the way disparate sets of objects interact and communicate with each other. Allows for the actions of each object set to vary independently of one another.

Use When:

  1. Communication between sets of objects is well defined and complex.
  2. Too many relationships exist and common point of control or communication is needed.
A

MEDIATOR

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

Allows for access to the elements of an aggregate object without allowing access to its underlying representation.

Use When

  1. Access to elements is needed without access to the entire representation.
  2. Multiple or concurrent traversals of the elements are needed.
  3. A uniform interface for traversal is needed.
  4. Subtle differences exist between the implementation details of various iterators.
A

ITERATOR

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

Lets one or more objects be notified of state changes in other objects within the system

Use When

  1. State changes in one or more objects should trigger behavior in other objects n Broadcasting capabilities are required.
  2. An understanding exists that objects will be blind to the expense of notification.
A

OBSERVER

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

Defines a set of encapsulated algorithms that can be swapped to carry out a specific behavior.

Use When

  1. The only difference between many related classes is their behavior.
  2. Multiple versions or variations of an algorithm are required.
  3. Algorithms access or utilize data that calling code shouldn’t be exposed to.
  4. The behavior of a class should be defined at runtime.
  5. Conditional statements are complex and hard to maintain.
A

STRATEGY

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

Ties object circumstances to its behavior, allowing the object to behave in different ways based upon its internal state.

Use When

  1. The behavior of an object should be influenced by its state.
  2. Complex conditions tie object behavior to its state.
  3. Transitions between states need to be explicit.
A

STATE

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

Identifies the framework of an algorithm, allowing implementing classes to define the actual behavior.

Use When

  1. A single abstract implementation of an algorithm is needed.
  2. Common behavior among subclasses should be localized to a common class.
  3. Parent classes should be able to uniformly invoke behavior in their subclasses.
  4. Most or all subclasses need to implement the behavior.
A

TEMPLATE METHOD

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

Allows for one or more operations to be applied to a set of objects at runtime, decoupling the operations from the object structure.

Use When

  1. An object structure must have many unrelated operations performed upon it.
  2. The object structure can’t change but operations performed on it can.
  3. Operations must be performed on the concrete classes of an object structure.
  4. Exposing internal state or operations of the object structure is acceptable.
  5. Operations should be able to operate on multiple object structures that implement the same interface sets.
A

VISITOR

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

Exception handling in some languages implements this pattern. When an exception is thrown in a method the runtime checks to see if the method has a mechanism to handle the exception or if it should be passed up the call stack. When passed up the call stack the process repeats until code to handle the exception is encountered or until there are no more parent objects to hand the request to.

A

CHAIN OF RESPONSIBILITY

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

Job queues are widely used to facilitate the asynchronous processing of algorithms. By utilizing the command pattern the functionality to be executed can be given to a job queue for processing without any need for the queue to have knowledge of the actual implementation it is invoking. The command object that is enqueued implements its particular algorithm within the confines of the interface the queue is expecting.

A

COMMAND

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

Text based adventures, wildly popular in the 1980’s, provide a good example of this. Many had simple commands, such as “step down” that allowed traversal of the game. These commands could be nested such that it altered their meaning. For example, “go in” would result in a different outcome than “go up”. By creating a hierarchy of commands based upon the command and the qualifier (non-terminal and terminal expressions) the application could easily map many command variations to a relating tree of actions

A

INTERPRETER

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

Allows users to traverse various types of data sets without worrying about the underlying implementation of the collection. Since clients simply interact with the iterator interface, collections are left to define the appropriate iterator for themselves. Some will allow full access to the underlying data set while others may restrict certain functionalities, such as removing items.

A

ITERATOR

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

Undo functionality can nicely be implemented using the memento pattern. By serializing and deserializing the state of an object before the change occurs we can preserve a snapshot of it that can later be restored should the user choose to undo the operation.

A

MEMENTO

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

This pattern can be found in almost every GUI environment. When buttons, text, and other fields are placed in applications the application typically registers as a listener for those controls. When a user triggers an event, such as clicking a button, the control iterates through its registered observers and sends a notification to each.

A

OBSERVER

17
Q

An email object can have various states, all of which will change how the object handles different functions. If the state is “not sent” then the call to send() is going to send the message while a call to recallMessage() will either throw an error or do nothing. However, if the state is “sent” then the call to send() would either throw an error or do nothing while the call to recallMessage() would attempt to send a recall notification to recipients. To avoid conditional statements in most or all methods there would be multiple state objects that handle the implementation with respect to their particular state. The calls within the Email object would then be delegated down to the appropriate state object for handling.

A

STATE

18
Q

A parent class, InstantMessage, will likely have all the methods required to handle sending a message. However, the actual serialization of the data to send may vary depending on the implementation. A video message and a plain text message will require different algorithms in order to serialize the data correctly. Subclasses of InstantMessage can provide their own implementation of the serialization method, allowing the parent class to work with them without understanding their implementation details.

A

TEMPLATE METHOD

19
Q

Calculating taxes in different regions on sets of invoices would
require many different variations of calculation logic. Implementing
a visitor allows the logic to be decoupled from the invoices and
line items. This allows the hierarchy of items to be visited by calculation
code that can then apply the proper rates for the region.
Changing regions is as simple as substituting a different visitor.

A

VISITOR

20
Q

Calculating taxes in different regions on sets of invoices would
require many different variations of calculation logic. Implementing
a visitor allows the logic to be decoupled from the invoices and
line items. This allows the hierarchy of items to be visited by calculation
code that can then apply the proper rates for the region.
Changing regions is as simple as substituting a different visitor.

A

VISITOR