


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 4
This page cannot be seen from the preview
Don't miss anything!



1
state of an object.publish/subscribe) is a design pattern used to observe theThe observer pattern (sometimes known as
observe that these display objectsthat display the data, andseparate from the objectscontaining the data isassumes that the objectThe Observer pattern (^) changes in that data.
5
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
This main window is the Subject or data repository object.
13
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
15
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
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