1 / 11

Observer Design Pattern

Observer Design Pattern. Source: Design Patterns – Elements of Reusable Object-Oriented Software; Gamma, et. al. Problem. An object (Observer) needs to be notified whenever another object (Subject) changes state so it can: Keep its own state in synch with the Subject or

alcina
Download Presentation

Observer Design Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Observer Design Pattern Source: Design Patterns – Elements of Reusable Object-Oriented Software; Gamma, et. al.

  2. Problem • An object (Observer) needs to be notified whenever another object (Subject) changes state so it can: • Keep its own state in synch with the Subject • or • Perform some action in response to changes in the Subject • Example: Synchronizing data in Inventory Tracker GUI

  3. Solution

  4. Solution

  5. Consequences • Can flexibly add and remove observers to an object • Subject is not tightly coupled to Observer • This is an example of dependency inversion • Supports broadcast communication • Changing the state of an object can be far more expensive than you expect

  6. Known Uses: Synchronizing data with graphical views • Example: Temperature Editor

  7. Known Uses: Session listeners • A Session object stores information about the current user session • What user is logged in and what their privileges are • Time at which the session started • History • URL history in web browser • Command history for undo/redo • Other parts of the application might need to be notified when the Session state changes • User logged in • User logged out

  8. Known Uses: Java Observers • Support for the Observer pattern is built into Java interface Observer { void update(Observable o, Object arg); } class Observable { void addObserver(Observer o) { … } void deleteObserver(Observer o) { … } void notifyObservers(Object arg) { … } }

  9. Java Observer import java.util.Observable; import java.util.Observer; public class Screen implements Observer { @Override public void update(Observable o, Object arg) { System.out.println("Changed to: " + ((DataStore)(o)).getData()); } }

  10. Java Observable import java.util.Observable; public class DataStore extends Observable { private String data; public String getData() { return data; } public void setData(String data) { this.data =data; //mark the observable as changed setChanged(); //send a notification notifyObservers(); } }

  11. Java Observer Pattern public class ObserverPattern { /** * @param args */ public static void main(String[] args) { Screen screen = new Screen(); DataStore dataStore = new DataStore(); //register observer dataStore.addObserver(screen); dataStore.setData("Hello World"); } }

More Related