observer n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Observer PowerPoint Presentation
Download Presentation
Observer

Loading in 2 Seconds...

play fullscreen
1 / 13

Observer - PowerPoint PPT Presentation


  • 178 Views
  • Uploaded on

Observer. Please Snarf the Code for Today’s Class. Today you will. Implement the observer pattern on some sample code Hear about a few more general issues with the observer pattern Take a look at Tivoo Part 3. The WeatherData example.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Observer' - livana


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
observer

Observer

Please Snarf the Code for Today’s Class.

today you will
Today you will
  • Implement the observer pattern on some sample code
  • Hear about a few more general issues with the observer pattern
  • Take a look at Tivoo Part 3
the weatherdata example
The WeatherData example
  • Go to “Head First Design Patterns Observer” in the resources section of Sakai, and read the problem description (pages 38-41)
  • Then take a look at the WeatherData class in the code you’ve snarfed. Come to a consensus with those sitting next to you about what’s wrong with it. You should be able to find at least 2 things.
what s wrong with the weatherdata class more than one may apply
What’s wrong with the WeatherData class? (more than one may apply)
  • The class handles two things: communicating with the remote weather station and dealing with the GUI
  • WeatherData is a data class
  • Duplicated code in the measurementsChanged method
  • This code is not “closed” in the situation where new kinds of weather displays are added
what we want
What We Want
  • Code for handling the model to be separate from code focused on displaying the data
  • But there’s a problem: oftentimes displays need to be updated when the model changes
  • So the model code needs to call the display code
  • So the model code needsto know about the display code
solution
Solution
  • So the model code needs to call the display code
  • So the model code needsto know about the display code
  • The model code allows other code to register it’s interest in updates (we say it allows Observers to register themselves)
  • Then the display code registers…this happens in the man or someplace outside of the model
  • Whenever a change happens, the model notifies all its observers
  • The key insight here is that although the model knows something probably receives updates, it does not care what kind of classes do that. So it is “open” to any new kind of observer
not just in guis
Not just in GUIs
  • Anyplace where you want notifications of what happens, but want to decouple the code that notifies from the code that acts on the notifications
push vs pull
Push vs. Pull
  • “Push” – the subject passes a lot of data about what has changed to it’s observers. Makes it easier for observers.
  • “Pull” – the subject just notifies the observers and then they query subject (or other state) to figure out how they ought to change. Allows subjects to be more generic.
be aware
Be Aware
  • Java has built-in observer objects for you to subclass from (java.util.Observable and java.util.Observer). They take of the drudge work of implementing observers. (HFDP pg. 64)
  • But don’t confuse the idea (pattern) of observer with Java’s implementation. Even when there’s a built in one, you’ll often want to roll your own.
limits of observers
Limits of Observers
  • When you have many data objects, all of which can be observed it can get difficult to set up the appropriate observer relationships
  • Occasionally the observer pattern can be used with data too – one part of the model is updated when another part changes. Be careful with this – you can get infinite update loops or other problems