Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

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

0
ambuli
Asked:
ambuli
1 Solution
 
for_yanCommented:

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
0
 
ambuliAuthor Commented:
Actually, I want this in C++.  sorry, I should not have added the Java zone.
0
 
evilrixSenior Software Engineer (Avast)Commented:
You want the Observer Pattern
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
evilrixSenior Software Engineer (Avast)Commented:
Just let observer subscribe and when an event happens notify them and let them decided whether to do something or not.
0
 
dpearsonCommented:
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.

Doug
0
 
ambuliAuthor Commented:
Thanks Doug.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now