Observer Design Pattern - PowerPoint PPT Presentation

About This Presentation
Title:

Observer Design Pattern

Description:

... (the publisher) and the object that needs a change notification (the subscriber) ... about subscribers only through an interface and subscribers can ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 11
Provided by: ericp87
Learn more at: http://www.cs.rpi.edu
Category:

less

Transcript and Presenter's Notes

Title: Observer Design Pattern


1
Observer Design Pattern
  • By Eric Perret
  • Pages 372-380 in Applying UML and Patterns

2
What we are covering.
  • Definition
  • Example
  • Example using code
  • Why it is a good idea to use the pattern.
  • Summary

3
What is an Observer?
  • The Observer design pattern, or more accurately
    some implementation of this pattern, is used to
    assure consistency between different objects.
  • AKA Publish-Subscribe

4
What does this really mean.
  • When an object changes state, all of the objects
    that need to know about it are notified and
    updated automatically.

5
Example
  • Let the state of an object "B" depend on the
    state of an object "A". Whenever the state of
    object "A" changes, "B" has to recompute its
    state in order to remain consistent with "A".
  • The observer design pattern supports such
    dependencies and at the same time tries to reduce
    the coupling between the object that changes (the
    publisher) and the object that needs a change
    notification (the subscriber).

6
Here's an example of the interfaces in java-like
code
  • public abstract interface Subject public
    abstract void addObserver(Observer
    observer) public abstract void
    removeObserver(Observer observer) public
    abstract void notify()public abstract class
    Observer public abstract void update()
  • The subject calls its "notify" operation whenever
    it changes. "Notify" calls the "update" operation
    on each observer. The observers then call back to
    the subject and update themselves accordingly.

7
Why Use the Pattern?
  • State dependencies between objects are
    unfavorable for several reasons.
  • If we could model our problem domain as a set of
    independent classes, we would get a software
    system without state dependencies.
  • Coding and especially maintaining such a system
    would be much easier. The programmer could focus
    on single classes only.

8
Summary
  • Loosely couple objects in terms of communication
  • Publishers know about subscribers only through an
    interface and subscribers can register or
    de-register dynamically with the publisher

9
Question, comments, theories, or conundrums?
  • Slides can be found here http//www.rpi.edu/perr
    ee/sdd

10
References
  • Applying UML and Patterns by Craig Larman,
    352-380
  • On Using the Observer Design Pattern by
    Constantin Szallies, http//www.wohnklo.de/pattern
    s/observer.html
  • Observer Design Pattern by Stephen Lam,
    http//sern.ucalgary.ca/courses/SENG/609.04/W98/la
    msh/observerLib.html
Write a Comment
User Comments (0)
About PowerShow.com