ambuli
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.
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_
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( );
}
ASKER
Actually, I want this in C++. sorry, I should not have added the Java zone.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Doug.
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