Questions about the observer pattern

S

samir.vds

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
 
H

hiwa

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
Use conditional in their update() method.
There would be no way for selective event delivery.
In other words, it is universal for observers.
 
G

garethconner

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
 
D

Deniz Dogan

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
 
B

Ben_

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top