Behavioral Patterns Flashcards
Gives more than one object an opportunity to handle a request by linking receiving objects together. Use When:
- Multiple objects may handle a request and the handler doesn’t have to be a specific object.
- A set of objects should be able to handle a request with the handler determined at runtime.
- A request not being handled is an acceptable potential outcome.
CHAIN OF RESPONSIBILITY
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:
- You need callback functionality
- Requests need to be handled at variant times or in variant orders.
- A history of requests is needed. n The invoker should be decoupled from the object handling the invocation.
COMMAND
Defines a representation for a grammar as well as a mechanism to understand and act upon the grammar.
Use When
- There is grammar to interpret that can be represented as large syntax trees.
- The grammar is simple.
- Efficiency is not important.
- Decoupling grammar from underlying expressions is desired.
INTERPRETER
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:
- Communication between sets of objects is well defined and complex.
- Too many relationships exist and common point of control or communication is needed.
MEDIATOR
Allows for access to the elements of an aggregate object without allowing access to its underlying representation.
Use When
- Access to elements is needed without access to the entire representation.
- Multiple or concurrent traversals of the elements are needed.
- A uniform interface for traversal is needed.
- Subtle differences exist between the implementation details of various iterators.
ITERATOR
Lets one or more objects be notified of state changes in other objects within the system
Use When
- State changes in one or more objects should trigger behavior in other objects n Broadcasting capabilities are required.
- An understanding exists that objects will be blind to the expense of notification.
OBSERVER
Defines a set of encapsulated algorithms that can be swapped to carry out a specific behavior.
Use When
- The only difference between many related classes is their behavior.
- Multiple versions or variations of an algorithm are required.
- Algorithms access or utilize data that calling code shouldn’t be exposed to.
- The behavior of a class should be defined at runtime.
- Conditional statements are complex and hard to maintain.
STRATEGY
Ties object circumstances to its behavior, allowing the object to behave in different ways based upon its internal state.
Use When
- The behavior of an object should be influenced by its state.
- Complex conditions tie object behavior to its state.
- Transitions between states need to be explicit.
STATE
Identifies the framework of an algorithm, allowing implementing classes to define the actual behavior.
Use When
- A single abstract implementation of an algorithm is needed.
- Common behavior among subclasses should be localized to a common class.
- Parent classes should be able to uniformly invoke behavior in their subclasses.
- Most or all subclasses need to implement the behavior.
TEMPLATE METHOD
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
- An object structure must have many unrelated operations performed upon it.
- The object structure can’t change but operations performed on it can.
- Operations must be performed on the concrete classes of an object structure.
- Exposing internal state or operations of the object structure is acceptable.
- Operations should be able to operate on multiple object structures that implement the same interface sets.
VISITOR
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.
CHAIN OF RESPONSIBILITY
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.
COMMAND
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
INTERPRETER
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.
ITERATOR
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.
MEMENTO