Results 1 to 4 of 4
  1. #1
    cloud_strife is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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.

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Quote Originally Posted by cloud_strife View Post
    Hi all,:)
    I read something about Observer model, do you think I should use it?
    Yes. This is an ideal situation for it.

    Quote Originally Posted by cloud_strife View Post
    Is it possible to use change listeners?:confused:
    Yes, but why? It would seem to make more sense to define your own listener interface. It's just 1 more class.

    Quote Originally Posted by cloud_strife View Post
    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?
    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

  1. Monitoring the network
    By khajalid in forum New To Java
    Replies: 4
    Last Post: 09-10-2008, 01:55 PM
  2. Monitoring the network
    By khajalid in forum Networking
    Replies: 3
    Last Post: 09-10-2008, 01:47 PM
  3. Monitoring the network
    By khajalid in forum Java Applets
    Replies: 0
    Last Post: 09-09-2008, 05:40 PM
  4. Monitoring the network
    By khajalid in forum Advanced Java
    Replies: 0
    Last Post: 09-09-2008, 02:35 PM
  5. Monitoring IP!
    By rameshraj in forum Advanced Java
    Replies: 4
    Last Post: 04-27-2008, 07:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •