Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

How to monitor some fields in an object and call a method when changed?

 
Ranch Hand
Posts: 101
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,

My question is:

Having the class Dog:



I would like to implement the observer pattern or something similar in a way that immediately when one of the three fields changes, the bark() method will be called.

Any idea?

Thanks in advance.

 
Saloon Keeper
Posts: 6798
162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not call the method directly from the setter methods?
 
Saloon Keeper
Posts: 7616
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two general comments:

I'm not fond of the redundancy in "dogName" when you already know that you are dealing with an object of type "Dog", just "name" should suffice.  Of course that means your constructor would need to have
this.name = name;

Your class is almost immutable. I wouldn't expect name and breed to change (I suppose name could change). Age would change a lot unless you stored it as something like birthYear or dateOfBirth and make getAge() calculate age on the fly. Then even age could be made immutable. Making your class immutable means you can do away with all the setter methods and keep the getter methods. This might eliminate the need for bark() unless you need it for another purpose.
 
Alfonso Sanz
Ranch Hand
Posts: 101
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Carey Brown: Thank you for your comments but it is just an example to show my concern.

@ Tim Moores: Thanks for your fast answer. You are right, but imagine the class having. a hundred of fields. I would like to know some mechanism that allows me to launch bark() when one of the fields mutate without having to write bark() in every setter method. I think it would be a more ellegant approach.

Thanks in advance.
 
Rancher
Posts: 4784
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even with an observer you would have to modify each set method to notify the observer that it has been called.
I mean, unless you used some sort of wrapper for each attribute (I think JavaFX does something like that with its properties?).
 
Tim Moores
Saloon Keeper
Posts: 6798
162
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could probably resort to Aspect-oriented programming (using something like AspectJ) with an aspect that gets triggered by all setXYZ methods in that class.
 
Alfonso Sanz
Ranch Hand
Posts: 101
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your answers.

I didn't think of AoP.

Perhaps PropertyChangeListener could work as well.
 
Saloon Keeper
Posts: 23006
156
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AOP is a bit messy to set up, but is the most transparent.

Another technique I use is to add a boolean "dirty" property to the bean and set dirty to true when any of the interesting property values is modified. Then I can simply call isDirty(). That works well when I may want to change multiple properties and only fire one event. Plus it doesn't require me to register listener callbacks for a PropertyChangeListener list.
 
You are HERE! The other map is obviously wrong. Better confirm with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic