part i 02. the observer pattern: keeping your objects in
TRANSCRIPT
![Page 1: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/1.jpg)
Part I 02. The Observer Pattern:
Keeping Your Objects in the Know
![Page 2: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/2.jpg)
Weather-O- Rama Project
February 25, 2014 2
![Page 3: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/3.jpg)
Overview - Weather-Monitoring Application
February 25, 2014 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 I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/4.jpg)
Unpacking the WeartherData Class
February 25, 2014 4
Our job is to implement measurementsChanged() so that it updates the three displays for current conditions, weather stats, and forecast.
![Page 5: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/5.jpg)
What do We Know so Far?
February 25, 2014 5
![Page 6: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/6.jpg)
Taking a First WeatherData Class
February 25, 2014 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 I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/7.jpg)
What’s Wrong with the First WeatherData Class
February 25, 2014 7
![Page 8: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/8.jpg)
Meet the Observer Pattern - Newspaper Subscription
February 25, 2014 8
![Page 9: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/9.jpg)
Publishers + Subscribers = Observer Pattern
February 25, 2014 9
Publisher
Subscriber
![Page 10: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/10.jpg)
Publishers + Subscribers = Observer Pattern (Cont.)
February 25, 2014 10
![Page 11: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/11.jpg)
Publishers + Subscribers = Observer Pattern (Cont.)
February 25, 2014 11
![Page 12: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/12.jpg)
Publishers + Subscribers = Observer Pattern (Cont.)
February 25, 2014 12
![Page 13: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/13.jpg)
The Observer Pattern Defined
February 25, 2014 13
![Page 14: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/14.jpg)
The Observer Pattern Defined - The Class Diagram
February 25, 2014 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 I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/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.
February 25, 2014 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 I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/16.jpg)
Designing the Weather Station
February 25, 2014 16
![Page 17: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/17.jpg)
Implementing the Weather Station
February 25, 2014 17
![Page 18: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/18.jpg)
Implementing the Subject Interface in WeatherData
February 25, 2014 18
![Page 19: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/19.jpg)
Implementing the Subject Interface in WeatherData (Cont.)
February 25, 2014 19
For testing
![Page 20: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/20.jpg)
Building the Display Elements
February 25, 2014 20
For future unregister
![Page 21: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/21.jpg)
Powering Up the Whether Station
February 25, 2014 21
![Page 22: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/22.jpg)
Using Java’s Built-in Observer Pattern
February 25, 2014 22
![Page 23: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/23.jpg)
How Java’s Built-in Observer Pattern Works
February 25, 2014 23
![Page 24: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/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.
February 25, 2014 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 I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/25.jpg)
Reworking WeatherData
February 25, 2014 25
![Page 26: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/26.jpg)
Reworking CurrentConditionDisplay
February 25, 2014 26
![Page 27: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/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.
February 25, 2014 27
![Page 28: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/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.
February 25, 2014 28
![Page 29: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/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.
February 25, 2014 29
![Page 30: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/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:
February 25, 2014 30
![Page 31: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/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.
February 25, 2014 31
![Page 32: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/32.jpg)
Design Principles in Observer Pattern
February 25, 2014 32
1 2
![Page 33: Part I 02. The Observer Pattern: Keeping Your Objects in](https://reader031.vdocument.in/reader031/viewer/2022012018/61daef5ee0c20c6f7f2db056/html5/thumbnails/33.jpg)
Conclusion
February 25, 2014 33