Questions about the observer pattern

Discussion in 'Java' started by samir.vds@googlemail.com, Oct 1, 2006.

  1. Guest

    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
     
    , Oct 1, 2006
    #1
    1. Advertising

  2. hiwa Guest

    wrote:
    > 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.
     
    hiwa, Oct 1, 2006
    #2
    1. Advertising

  3. Guest

    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


    wrote:
    > 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
     
    , Oct 1, 2006
    #3
  4. Deniz Dogan Guest

    wrote:
    > 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
     
    Deniz Dogan, Oct 1, 2006
    #4
  5. Guest

    Ok thanks, this helped me a lot guys :)
     
    , Oct 1, 2006
    #5
  6. Ben_ Guest

    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.
     
    Ben_, Oct 1, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Beatrice Rutger
    Replies:
    0
    Views:
    750
    Beatrice Rutger
    Jun 5, 2005
  2. Replies:
    6
    Views:
    749
    Chris Uppal
    Feb 13, 2006
  3. mem

    Observer pattern

    mem, May 18, 2004, in forum: C++
    Replies:
    2
    Views:
    545
    adiian
    Jun 4, 2007
  4. Paolino

    An observer pattern application.

    Paolino, Aug 18, 2005, in forum: Python
    Replies:
    0
    Views:
    349
    Paolino
    Aug 18, 2005
  5. Michael Schneider

    weakrefs to functions for observer pattern

    Michael Schneider, Nov 3, 2005, in forum: Python
    Replies:
    3
    Views:
    386
    Alex Martelli
    Nov 3, 2005
Loading...

Share This Page