Observer Design Pattern: Implementing Data Observation in Java - Prof. Atif M. Memon, Study notes of Programming Languages

An introduction to the observer design pattern, a software design pattern used to observe the state of an object and update multiple displays or observers in real-time. An explanation of the pattern, its assumptions, philosophy, interfaces, and an example implementation in java. It covers the creation of a main window as the subject, registration of observers, and the sendnotify() method.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-mla-3
koofers-user-mla-3 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2007
Observer Pattern
Apr. 17, 2007
2
What is it? And an Example
The observer pattern (someti mes known as
publish/subscribe) is a design pattern used to observe the
state of an object.
For example, we often like to display data in more than
one form at the same time and have all of the displays
reflect any changes in that data.
For example, you might repre sent stock price changes both as a
graph and as a table or list box.
Each time the price changes, we’d expect both representations to
change at once without any ac tion on our part.
In Java we can easily make use of the Observer Design Pattern to
cause our program to behave in th is way.
3
“The Observer Pattern Assumption”
The Observer pattern
assumes that the object
containing the data is
separate from the object s
that display the data, and
that these display objects
observe changes in that
data.
4
The Observer Philosophy
We usually refer to the data as the Subject and
each of the displays as Observers.
Each of these observers registers its interest in the
data by calling a public method in the Subject.
Then, each observer has a known interface that the
subject calls when the data change.
pf3
pf4

Partial preview of the text

Download Observer Design Pattern: Implementing Data Observation in Java - Prof. Atif M. Memon and more Study notes Programming Languages in PDF only on Docsity!

1

CMSC 433 – Programming Language

Technologies and Paradigms

Observer PatternSpring 2007

Apr. 17, 2007

What is it? And an Example

state of an object.publish/subscribe) is a design pattern used to observe theThe observer pattern (sometimes known as

  • reflect any changes in that data.one form at the same time and have all of the displaysFor example, we often like to display data in more than cause our program to behave in this way.–^ In Java we can easily make use of the Observer Design Pattern tochange at once without any action on our part.–^ Each time the price changes, we’d expect both representations tograph and as a table or list box.^ For example, you might represent stock price changes both as a 3

“The Observer Pattern Assumption”

observe that these display objectsthat display the data, andseparate from the objectscontaining the data isassumes that the objectThe Observer pattern (^) changes in that data.

The Observer Philosophy

We usually refer to the data as the

Subject

and

each of the displays as

Observers

data by calling a public method in the Subject.Each of these observers registers its interest in the

subject calls when the data change.Then, each observer has a known interface that the

5

Observer and Subject Interfaces

else they do.objects to be of type Subject and Observer no matter whatimplement these interfaces, and that we can declare thesewrite any sort of class objects we want as long as theyLets define some abstract interfaces. They will allow us to

An Example

  • display in two windows:button choice changes theThe user-controlled radio- box.the new color to the list–^ Second, adds the name oftext string in the first panel.background color and the^ First, changes the 7

Lets Create the Main Window!

This main window is the Subject or data repository object.

More Main Window Code

13

Invoking sendNotify()

program calls theEvery time someone clicks on one of the radio buttons, the main (^) sendNotify (^) method of each Observer who has objects in the observers Vector.registered interest in these changes by simply running through the

sendNotify() Explained

and the text string in the frame panel.sendNotify method changes the background colorIn the case of the ColorFrame observer, the

just adds the name of the new color to the list box.In the case of the ListFrame observer, however, it

15

Notification Type

message is the string representing the color itself.In this carefully constructed example, the notification

caption for that button and send it to the observers.When we click on one of the radio buttons, we can get the

that string representation.This, of course, assumes that all the observers can handle

observe other data objects.case, especially if the observers could also be used toIn more realistic situations, this might not always be the

that demand specific, but different, kinds of data.In more complicated systems, we might have observers

Notification Type (contd…)

to perform this conversion.right data type, we could use an intermediate Adapter classRather than have each observer convert the message to the

in several ways.case where the data of the central subject class can changeAnother problem observers may have to deal with is the

or change the scale of the data we are viewing.We could delete points from a list of data, edit their values,

occurred.then have the observer ask which sort of change hasmessages to the observers or send a single message andIn these cases we either need to send different change