Listener implementation method

Posted on 2011-10-30
Last Modified: 2012-05-12
Hi Experts,
I have an object with few attributes.  The attributes are strings, int, or anotherObject. Interested parties should register listeners for a particular attribute. For example, if someone is interested in attrOne should call addListener( ) indicating it.   How can I implement this?  I am looking for some pseudo code type advice.  

I was thinking may be I should add an enum to the class, like
and then the listeners will be calling like
addListener(Object.ATTRIB_ONE, callback );
Is that a good way?

Thank you very much.

class Object
     String attrOne;
     String attrTwo;
     int  attrThree;
     anotherObject attrFour;
     void addListener( , callback );
     void removeListener( );
     void notifyListeners( );

Open in new window

Question by:ambuli
    LVL 47

    Expert Comment


    If you are talking about Java you can make a Java Bean of your class and register ChangeProperty Listenner - read more here:

    Author Comment

    Actually, I want this in C++.  sorry, I should not have added the Java zone.
    LVL 39

    Expert Comment

    You want the Observer Pattern
    LVL 39

    Expert Comment

    Just let observer subscribe and when an event happens notify them and let them decided whether to do something or not.
    LVL 26

    Accepted Solution

    ambuli what you're proposing - using an enum and having listeners pass in the events they are interested in is basically fine.  The main problem will be handling listeners who are interested in multiple attributes.  You need to make sure that a listener can call addListener(attribute, callback) multiple times to listen to multiple attributes.

    If that seems tricky in your case another design would be to use a bitfield to indicate the attributes you're interested in:

    int attributeOne  = 1 << 0 ;
    int attributeTwo  = 1 << 1 ;
    int attributeThree = 1 << 2 ;

    Then a caller could register for multiple events in a single call:

    addListener(attributeOne | attributeThree, callback) ;

    If you're not comfortable with bitfields and bit twiddling in general you should probably avoid this path, but it is another reasonable design.


    Author Closing Comment

    Thanks Doug.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now