Link to home
Start Free TrialLog in
Avatar of ambuli
ambuliFlag for United States of America

asked on

Listener implementation method

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
enum{ ATTRIB_ONE, ATTRIB_TWO, ATTRIB_THREE, ATTRIB_FOUR }
and then the listeners will be calling like
addListener(Object.ATTRIB_ONE, callback );
Is that a good way?

Thank you very much.

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

Open in new window

Avatar of for_yan
for_yan
Flag of United States of America image


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

http://download.oracle.com/javase/tutorial/uiswing/events/propertychangelistener.html
Avatar of ambuli

ASKER

Actually, I want this in C++.  sorry, I should not have added the Java zone.
Avatar of evilrix
You want the Observer Pattern
Just let observer subscribe and when an event happens notify them and let them decided whether to do something or not.
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ambuli

ASKER

Thanks Doug.