Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Questions about the observer pattern

Thread view: 
samir.vds@googlemail.com - 01 Oct 2006 02:58 GMT
Hi,

I'm an ongoing student in Business Information Systems.

Anyway, right now I'm trying to understand a design technique called
the observer pattern.

Java provides for the observable object the class
java.util.oberservable. If I want to notifiy my observers about an
event I call the method notifyObservers() to notifiy all my observers.
So in example I've an observable object, which has 2 registered
observers. The observable object contains a couple of events, which
notifies my observers, but some of those events only want to notify one
of the observers. So what's smartest way to do that ?

regards Samir

btw: I hope you understand my problem, because it's very concrete and
also my English is not the best :D
hiwa - 01 Oct 2006 03:19 GMT
> Hi,
>
[quoted text clipped - 15 lines]
> btw: I hope you understand my problem, because it's very concrete and
> also my English is not the best :D
Use conditional in their update() method.
There would be no way for selective event delivery.
In other words, it is universal for observers.
garethconner@mac.com - 01 Oct 2006 03:33 GMT
Hi Samir,

An alternative to using the java.util.observable class is to roll your
own.  Create your own listener interface for the observers and build
your observable class with methods for adding and removing listeners.
This listener interface could have mutliple notifications i.e.:

notifyActionA
notifyActionB

The observer classes that are interested in these events would have to
implement the listener interface.  However if a class is not interested
in ActionA, it can leave the implementation for that method empty.

The swing classes often use this pattern, for example a JFrame will
accept MouseListeners.  Classes that implement the MouseListener
interface can receive various mouse events (mouseClicked, mouseEntered,
etc.), but can simply ignore mouse events that aren't needed.

Hope that helps, I'm a novice with Java so others may have better
suggestions.

-Gareth

> Hi,
>
[quoted text clipped - 15 lines]
> btw: I hope you understand my problem, because it's very concrete and
> also my English is not the best :D
Deniz Dogan - 01 Oct 2006 11:54 GMT
> The observable object contains a couple of events, which
> notifies my observers, but some of those events only want to notify one
> of the observers. So what's smartest way to do that ?

Hello, Samir!

The way I would go about doing what you want is using the Object
parameter of the update(Observable, Object) method:

public void update(Observable o, Object arg) {
    if (arg somethingsomething) {
        //We know it's for Observer no. 1
    }
    else if (arg somethingelse) {
        //It's for Observer no. 2
    }
}

On the other hand, I'm not the best of Java programmers and this may in
fact be a weird solution.

> btw: I hope you understand my problem, because it's very concrete and
> also my English is not the best :D

Your English is just fine, don't worry about it. :-)

- Deniz Dogan
samir.vds@googlemail.com - 01 Oct 2006 19:27 GMT
Ok thanks, this helped me a lot guys :)
Ben_ - 01 Oct 2006 20:07 GMT
How do you determine that one of the observers is not interested ?

If an observer registers, it's to be notified, and it will look at the
observed object to determine what neeeds done. It's not the observed object
than can know what the observer wants to do.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.