33
Part I 02. The Observer Pattern: Keeping Your Objects in the Know

Part I 02. The Observer Pattern: Keeping Your Objects in

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Part I 02. The Observer Pattern: Keeping Your Objects in

Part I 02. The Observer Pattern:

Keeping Your Objects in the Know

Page 2: Part I 02. The Observer Pattern: Keeping Your Objects in

Weather-O- Rama Project

February 25, 2014 2

Page 3: Part I 02. The Observer Pattern: Keeping Your Objects in

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

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

What do We Know so Far?

February 25, 2014 5

Page 6: Part I 02. The Observer Pattern: Keeping Your Objects in

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

What’s Wrong with the First WeatherData Class

February 25, 2014 7

Page 8: Part I 02. The Observer Pattern: Keeping Your Objects in

Meet the Observer Pattern - Newspaper Subscription

February 25, 2014 8

Page 9: Part I 02. The Observer Pattern: Keeping Your Objects in

Publishers + Subscribers = Observer Pattern

February 25, 2014 9

Publisher

Subscriber

Page 10: Part I 02. The Observer Pattern: Keeping Your Objects in

Publishers + Subscribers = Observer Pattern (Cont.)

February 25, 2014 10

Page 11: Part I 02. The Observer Pattern: Keeping Your Objects in

Publishers + Subscribers = Observer Pattern (Cont.)

February 25, 2014 11

Page 12: Part I 02. The Observer Pattern: Keeping Your Objects in

Publishers + Subscribers = Observer Pattern (Cont.)

February 25, 2014 12

Page 13: Part I 02. The Observer Pattern: Keeping Your Objects in

The Observer Pattern Defined

February 25, 2014 13

Page 14: Part I 02. The Observer Pattern: Keeping Your Objects in

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

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

Designing the Weather Station

February 25, 2014 16

Page 17: Part I 02. The Observer Pattern: Keeping Your Objects in

Implementing the Weather Station

February 25, 2014 17

Page 18: Part I 02. The Observer Pattern: Keeping Your Objects in

Implementing the Subject Interface in WeatherData

February 25, 2014 18

Page 19: Part I 02. The Observer Pattern: Keeping Your Objects in

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

Building the Display Elements

February 25, 2014 20

For future unregister

Page 21: Part I 02. The Observer Pattern: Keeping Your Objects in

Powering Up the Whether Station

February 25, 2014 21

Page 22: Part I 02. The Observer Pattern: Keeping Your Objects in

Using Java’s Built-in Observer Pattern

February 25, 2014 22

Page 23: Part I 02. The Observer Pattern: Keeping Your Objects in

How Java’s Built-in Observer Pattern Works

February 25, 2014 23

Page 24: Part I 02. The Observer Pattern: Keeping Your Objects in

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

Reworking WeatherData

February 25, 2014 25

Page 26: Part I 02. The Observer Pattern: Keeping Your Objects in

Reworking CurrentConditionDisplay

February 25, 2014 26

Page 27: Part I 02. The Observer Pattern: Keeping Your Objects in

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

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

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

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

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

Design Principles in Observer Pattern

February 25, 2014 32

1 2

Page 33: Part I 02. The Observer Pattern: Keeping Your Objects in

Conclusion

February 25, 2014 33