Results 1 to 4 of 4
Thread: monitoring an object changes
- 03-14-2009, 02:35 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 1
- Rep Power
- 0
monitoring an object changes
Hi all,:)
I am developing a standard Java application.
I have a class representing a quite complex data structure with some private members and corresponding setters/getters.
I wish to create a method (in another class) which is called anytime some attribute of my data structure class changes.
I read something about Observer model, do you think I should use it?
Is it possible to use change listeners?:confused:
Is it possible to avoid to put an istruction like "notifyobserver()" at the end of every setter methods in order to call the "update" method?
Thank you all.:)
Bye
-
Yes, the observable pattern might work quite well for you here. Myself, I'm not a big fan of Java's hard-coded implementation of this pattern since it requires you to subclass Observable. I'd much rather have Observable be an interface, and so I usually write my own interfaces for this pattern.
- 03-14-2009, 09:28 PM #3
The only alternative to having some kind of notifyObserver() is to periodically poll the values you are looking at. However, in general this wastes lots of CPU time and you have no guarantees of spotting every change.
- 03-15-2009, 06:39 AM #4
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Yes. This is an ideal situation for it.
Yes, but why? It would seem to make more sense to define your own listener interface. It's just 1 more class.
If there are so many setter methods that this is a burden, then your design is probably not sufficiently encapsulated. An ideal design has no getter or setter methods at all. In practice they are needed, but use them sparingly. If there is no good reason to expose your member variables, then don't.
...but, if you're sitution is such that you need such exposure (for example, a class that represents data stored in a relational database) and it's a burden, you could use aspects to accomplish this. I haven't used it, but you might look into AspectJ.
Similar Threads
-
Monitoring the network
By khajalid in forum New To JavaReplies: 4Last Post: 09-10-2008, 01:55 PM -
Monitoring the network
By khajalid in forum NetworkingReplies: 3Last Post: 09-10-2008, 01:47 PM -
Monitoring the network
By khajalid in forum Java AppletsReplies: 0Last Post: 09-09-2008, 05:40 PM -
Monitoring the network
By khajalid in forum Advanced JavaReplies: 0Last Post: 09-09-2008, 02:35 PM -
Monitoring IP!
By rameshraj in forum Advanced JavaReplies: 4Last Post: 04-27-2008, 07:50 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks