Listing Vault SE322 · chapter 07 · Behavioral Design Patterns

Chapter 07 — Behavioral Design Patterns

How behavior and communication are distributed among objects. The lecture covers Iterator and Observer in detail and names the rest of the family for recognition.

07chapter
lists
total items
07

Behavioral Design Patterns

13 lists

Definition & Mnemonic I.O.

  1. Behavioral patterns are responsible for the efficient and safe distribution of behaviors among a program's objects
  2. Iterate — Iterator gives a standard way to walk a collection
  3. Observe — Observer gives a standard way to watch an object for changes

The Behavioral Family 11

Covered in detail: Iterator and Observer. The rest are named so you recognize them as behavioral.

  1. Chain of Responsibility
  2. Command
  3. Interpreter
  4. Iterator — covered
  5. Mediator
  6. Memento
  7. Observer — covered
  8. State
  9. Strategy
  10. Template Method
  11. Visitor

Iterator — Intent 2 keys

  1. Access the elements of a collection sequentially, one after another in a defined order
  2. Without knowing the representation — array, linked list or tree, no client breaks

Iterator — Participants 5

  1. Client — talks to the Aggregate and Iterator interfaces only
  2. Aggregate — declares createIterator()
  3. ConcreteAggregate — returns a concrete iterator over its own data
  4. Iterator — declares hasNext() and next()
  5. ConcreteIterator — holds the position and accesses the concrete aggregate

Iterator — Build Order (I.C.C.A.) 5 steps

  1. I — design the Iterator interface
  2. C — design a concrete Iterator for each collection structure
  3. Implement its methods in terms of that data structure
  4. C — create the Collection interface with the iterator-creating method
  5. A — implement the Aggregate interface in each collection class

Iterator — Benefits 3

  1. A consistent way for clients to iterate over any collection
  2. Abstracts the internals — collections can change without changing clients
  3. Extensible — many iterators for different traversals (standard, reverse, filtered)

Iterator — The Key Detail 1

  1. The position lives on the iterator, not the collection — so several iterators can walk one collection independently

Observer — Intent 3 keys

  1. One-to-many — one Subject, many Observers
  2. Dependency — observers care about the subject's state; the subject does not care what they do with it
  3. Automatically — nobody polls; the subject pushes the notification on state change

Observer — Participants 4

  1. Subject — holds the observer list; attach(), detach(), notifyAll()
  2. ConcreteSubject — holds the state and notifies on change
  3. Observer — declares the abstract update()
  4. ConcreteObserver — implements update() and registers itself

Observer — Build Order (S.I.O.I.R.) 5 steps

  1. S — design the Subject interface with attach, detach and notify
  2. I — inherit from it in classes holding information of interest
  3. O — design the Observer interface with an abstract update()
  4. I — implement update() in every observer
  5. R — register observers at run time; the subject iterates and calls update on change

Observer — Benefits 2

  1. Flexibility to add new services without touching the Subject
  2. Services are compartmentalized, so maintaining and modifying them is easier

Iterator vs Observer compare

IteratorObserver
SolvesWalking a collection without knowing how it stores elementsMany objects reacting the moment one object's state changes
RelationshipOne client to one traversal at a timeOne Subject to many Observers
Cue phrase"access the elements sequentially without exposing the representation""one-to-many dependency, notified automatically"
Where they meetThe Subject iterates its observer list inside notifyAllObservers()

The Three Categories, Side by Side 3

CategoryConcerned withThis course's patterns
CreationalHow objects get createdAbstract Factory, Factory Method, Singleton, Builder, Prototype
StructuralHow classes and objects are composed into larger structuresAdapter, Composite, Facade
BehavioralHow behavior and communication are distributedIterator, Observer