Part II 02. The Observer Pattern: Keeping Your Objects in ...
Transcript of Part II 02. The Observer Pattern: Keeping Your Objects in ...
![Page 1: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/1.jpg)
Part II 02. The Observer Pattern:
Keeping Your Objects in the Know
![Page 2: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/2.jpg)
Weather-O- Rama Project
April 30, 2013 2
![Page 3: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/3.jpg)
Overview- Weather-Monitoring Application
April 30, 2013 3
The WeatherData object knows how to talk to the physical Weather Station, to get updated data. The WeatherData object then updates its displays for the three different display elements: Current Conditions (shows temperature, humidity, and pressure), Weather Statistics, and a simple forecast.
![Page 4: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/4.jpg)
Unpacking the WeartherData Class
April 30, 2013 4
Our job is to implement measurementsChanged() so that it updates the three displays for current conditions, weather stats, and forecast.
![Page 5: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/5.jpg)
What do We Know so Far?
April 30, 2013 5
![Page 6: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/6.jpg)
Taking a First WeatherData Class
April 30, 2013 6
• Issues: – We are coding to concrete implementations, not interfaces. – For every new display element we need to alter code. – We have no way to add (or remove) display elements at run time. – We haven’t encapsulated the part that changes.
![Page 7: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/7.jpg)
What’s Wrong with the First WeatherData Class
April 30, 2013 7
![Page 8: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/8.jpg)
Meet the Observer Pattern - Newspaper Subscription
April 30, 2013 8
![Page 9: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/9.jpg)
Publishers + Subscribers = Observer Pattern
April 30, 2013 9
![Page 10: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/10.jpg)
Publishers + Subscribers = Observer Pattern (Cont.)
April 30, 2013 10
![Page 11: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/11.jpg)
Publishers + Subscribers = Observer Pattern (Cont.)
April 30, 2013 11
![Page 12: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/12.jpg)
Publishers + Subscribers = Observer Pattern (Cont.)
April 30, 2013 12
![Page 13: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/13.jpg)
The Observer Pattern Defined
April 30, 2013 13
![Page 14: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/14.jpg)
The Observer Pattern Defined - The Class Diagram
April 30, 2013 14
The observers are dependent on the subject to update them when the data changes. This leads to a cleaner OO design than allowing many objects to control the same data.
![Page 15: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/15.jpg)
The Power of Loose Coupling
• The only thing the subject knows about an observer is that it implements a certain interface (the Observer interface).
• We can add new observers at any time.
• We never need to modify the subject to add new types of observers.
• We can reuse subjects or observers independently of each other.
• Changes to either the subject or an observer will not affect the other.
April 30, 2013 15
Loosely coupled designs allow us to build flexible OO systems that can handle change because they minimize the interdependency between objects.
![Page 16: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/16.jpg)
Designing the Weather Station
April 30, 2013 16
![Page 17: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/17.jpg)
Implementing the Weather Station
April 30, 2013 17
![Page 18: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/18.jpg)
Implementing the Subject Interface in WeatherData
April 30, 2013 18
![Page 19: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/19.jpg)
Implementing the Subject Interface in WeatherData (Cont.)
April 30, 2013 19
![Page 20: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/20.jpg)
Building the Display Elements
April 30, 2013 20
For future unregister
![Page 21: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/21.jpg)
Powering Up the Whether Station
April 30, 2013 21
![Page 22: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/22.jpg)
Using Java’s Built-in Observer Pattern
April 30, 2013 22
![Page 23: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/23.jpg)
How Java’s Built-in Observer Pattern Works
April 30, 2013 23
![Page 24: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/24.jpg)
Why setChanged() • The setChanged() method is used to signify that the state has changed and that
notifyObservers(), when it is called, should update its observers. If notifyObservers() is called without first calling setChanged(), the observers will NOT be notified.
April 30, 2013 24
• The setChanged() method is meant to give you more flexibility in how you update observers by allowing you to optimize the notifications. For example, in our weather station, imagine if our measurements were so sensitive that the temperature readings were constantly fluctuating by a few tenths of a degree. That might cause the WeatherData object to send out notifications constantly. Instead, we might want to send out notifications only if the temperature changes more than half a degree and we could call setChanged() only after that happened.
![Page 25: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/25.jpg)
Reworking WeatherData
April 30, 2013 25
![Page 26: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/26.jpg)
Reworking CurrentConditionDisplay
April 30, 2013 26
![Page 27: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/27.jpg)
Running the New Code
• Never depend on order of evaluation of the Observer notifications.
– The java.util.Observable has implemented its notifyObservers() method such that the Observers are notified in a different order than our own implementation.
April 30, 2013 27
![Page 28: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/28.jpg)
The Dark Side of java.util.Observable • Observable is a class
– First, because Observable is a class, you have to subclass it. - You can’t add on the Observable behavior to an existing class that already
extends another superclass. - This limits its reuse potential (and isn’t that why we are using patterns in the first
place?). – Second, because there isn’t an Observable interface, you can’t even create
your own implementation that plays well with Java’s built-in Observer API. - Nor do you have the option of swapping out the java.util implementation for
another (say, a new, multithreaded implementation).
• Observable protects crucial methods – This means you can’t call setChanged() unless you’ve subclassed
Observable or are in the same class of Observable.
April 30, 2013 28
![Page 29: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/29.jpg)
The Observation Pattern in the JDK • Both JavaBeans and Swing also provide their own
implementations of the observer pattern.
• Let’s take a look at a simple part of the Swing API, the JButton. If you look under the hood at JButton’s superclass, AbstractButton, you’ll see that it has a lot of add/remove listener methods. These methods allow you to add and remove observers, or as they are called in Swing, listeners, to listen for various types of events that occur on the Swing component. For instance, an ActionListener lets you “listen in” on any types of actions that might occur on a button, like a button press. You’ll find various types of listeners all over the Swing API.
April 30, 2013 29
![Page 30: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/30.jpg)
A Little Application with JButton You’ve got a button that says “Should I do it?” and when you click on that button the listeners (observers) get to answer the question in any way they want. We’re implementing two such listeners, called the AngelListener and the DevilListener. Here’s how the application behaves:
April 30, 2013 30
![Page 31: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/31.jpg)
A Little Application with JButton (Cont.) • All we need to do is create a JButton object, add it to a JFrame and set up our listeners.
April 30, 2013 31
![Page 32: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/32.jpg)
Design Principles in Observer Pattern
April 30, 2013 32
![Page 33: Part II 02. The Observer Pattern: Keeping Your Objects in ...](https://reader031.fdocuments.us/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db057/html5/thumbnails/33.jpg)
Conclusion
April 30, 2013 33