Design Patterns: Observer

Software Engineering
1.0x

Design Patterns: Observer

Created 3 years ago

Duration 0:10:24
lesson view count 571
Select the file type you wish to download
Slide Content
  1. Design Patterns: Observer

    Slide 1 - Design Patterns: Observer

    • Emerson Murphy-Hill
  2. Observer Pattern

    Slide 2 - Observer Pattern

    • Name
    • Observer
    • Description
    • Defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically
    • Problem description
    • Used when multiple objects require notification when state changes.
    • Don’t know what objects require information about the underlying state
    • Solution description
    • See a later slide with UML description
  3. Patterns by Example:Multiple displays enabled by Observer

    Slide 3 - Patterns by Example:Multiple displays enabled by Observer

    • A=10%
    • B=40%
    • C=30%
    • D=20%
    • Application data
    • A
    • B
    • C
    • D
    • A
    • D
    • C
    • B
    • Relative Percentages
    • Y 10 40 30 20
    • X 15 35 35 15
    • Z 10 40 30 20
    • A B C D
    • Change notification
    • Requests, modifications
  4. Observer Pattern

    Slide 4 - Observer Pattern

    • Subject
    • attach (Observer)
    • detach (Observer)
    • notify ()
    • Observer
    • update(Subject, StateInfo)
    • Concrete Observer
    • update(Subject, StateInfo)
    • Concrete Subject
    • setState()
    • StateInfo subjectState
    • observers
    • For all x in observers{
    • x  update(this, stateInfo); }
    • 1
    • *
    • Different variants of observer patterns allow different ways of passing subjectState (a StateInfo object) to an observer
  5. Observer Pattern - Participants

    Slide 5 - Observer Pattern - Participants

    • Subject
    • Has a list of observers;
    • interfaces for attaching/detaching an observer
    • Observer
    • An updating interface for objects that gets notified of changes in a subject.
    • ConcreteSubject
    • Stores state of interest to observers
    • Sends notification when state changes.
    • ConcreteObserver
    • Implements updating interface.
  6. Observer Pattern - Implementation

    Slide 6 - Observer Pattern - Implementation

    • interface Observer {
    • void update (Observable sub, Object arg)
    • }
    • Java terminology for Subject.
    • public void attach(Observer o) {}
    • public void detach(Observer o) {}
    • public void notify (Object arg) {}
    • class Observable {
    • }
    • public boolean hasChanged() {}
    • Sometimes we use names of
    • addListener
    • removeListener
    • notifyListeners
    • Instead of
    • attach
    • detach
    • notify
    • See example later
    • Sometimes we can omit the argument of notify if the information to be passed to the observers has been stored in an attribute of the Observerable object, See example later
    • We can have additional hasChanged method for Observerable
  7. Observer Pattern - Implementation

    Slide 7 - Observer Pattern - Implementation

    • class PiChartView implements Observer {
    • void update(Observable sub, Object arg) {
    • // repaint the pi-chart
    • }
    • }
    • A Concrete Observer.
    • class StatsTable extends Observable {
    • public boolean hasChanged() {
    • // override to decide when it
    • //is considered changed
    • }
    • }
    • A Concrete Subject.
  8. Observer Pattern - Consequences

    Slide 8 - Observer Pattern - Consequences

    • Abstract coupling between subject and observer. (subject need not know concrete observers)
    • Support for broadcast communication (all observers are notified)
    • Unexpected updates (observers need not know when updates occur)
  9. Another Example: Subject-Observer Interfaces

    Slide 9 - Another Example: Subject-Observer Interfaces

    • public interface Observer { public void update(String messageText);
    • }
    • public interface Subject {
    • public void addListener(Observer o); public void removeListener(Observer o); public void notifyListeners();
    • }
    • Here we don’t put a Subject object as the first argument of update since this example’s Observer doesn’t require to access info of a Subject object other than the passed messageText string.
  10. Example: Concrete Subjects

    Slide 10 - Example: Concrete Subjects

    • public class TownCrier implements Subject {
    • private List townResident = new ArrayList(); //obs.
    • private String messageText; //state info to pass
    • // this message is added so I can give // this class a reason to call notifyListener.
    • public void setMessage(String message){ System.out.println("I'm the Town Crier and " + "I've got a message: " + message); this.messageText = message; this.notifyListeners();
    • } public void addListener(Observer o) { this.townResident.add(o);
    • }
    • //Continued on next slide …
  11. Example: Concrete Subjects cont.

    Slide 11 - Example: Concrete Subjects cont.

    • public void removeListener(Observer o) { if (this.townResident.contains(o)){ this.townResident.remove(o);
    • }
    • } // call the update method on // each observer (town resident) public void notifyListeners() { for (Iterator iter townResident.iterator(); iter.hasNext();) {
    • Observer listener = (Observer) iter.next(); listener.update(messageText);
    • }
    • }
    • }
  12. Example: Concrete Observers

    Slide 12 - Example: Concrete Observers

    • public class TownResident implements Observer { public void update(String messageText) { System.out.println("Greetings my name is: " + this); System.out.println("I heard: " + messageText);
    • }
    • }
    • public class TownResident2 implements Observer { public void update(String messageText) { System.out.println("Greetings my name is: " + this); System.out.println("I heard: " + messageText);
    • }
    • }
  13. Example: Main Method

    Slide 13 - Example: Main Method

    • public class TestObservers {
    • public static void main(String args[]) {
    • // Create the Subject and Observers.
    • TownCrier s = new TownCrier();
    • TownResident observer = new TownResident ();
    • TownResident2 observer2 = new TownResident2 ();
    • // Add those Observers!
    • s.addObserver(observer);
    • s.addObserver(observer2);
    • // Make changes to the Subject.
    • s.setMessage(“I am here");
    • }