Link to home
Start Free TrialLog in
Avatar of bnrtech
bnrtech

asked on

Public Variable Change

I have a Java project that I recently inherited and admittedly my Java skills are very rusty.  In this code there are several public variables.  Moving forward we would like to change these to private with getters and setters.  The problem lies in that there are many pieces of legacy code still in use by others, that I do not have access to, that access these public variables directly.  We are looking to add in an adapter layer of code so that their interface does not actually change, but we are stuck on how to update the variables.  Is there a way to detect when a public variables value has changed?  I was hoping there might be some type of listener implementation to handle this but I am having no luck right now.  Any suggestions?
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

I think setter method itself could be a good listener to a change in variable value.
ASKER CERTIFIED SOLUTION
Avatar of Hegemon
Hegemon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Maybe a solution can be to check your public variable every time you access your private variable.

Or you could schedule a timer to check for changes at regular intervals.
Avatar of Holger101497
Holger101497

Well, I'm afraid there is no "clean interceptor" solution.  How many variables are we talking about here? 10? 50? 1000? :-)

Calling a method usually "does something". Changing a variable by itself does not have any effect (until the variable is used). If you're planning to add "an adapter layer of code", that adapter layer could still have the same public variables and the "layer beneath" (implementation layer) checks these variables right before they are used.

Depending on your implementation and the number of accesses to these variables, that may or may not be a viable solution / workaround... (btw: I do not recommend a background thread checking for modifications as that is "expensive" (performance wise) and really hard to debug. You can usually find good places / times in the code where it's worth checking (and the results are more reproducible and more clearly defined)).
I guess my first suggestion would involve triggering an event when the getter for the private variable are called. The adapter would be registered as a listener. The listener will then have to update the private variable based on the public variable's value.  Not sexy, but I think it will work.
you may use delegate pattern, for e.g.,

you define an adapter by holding referenc to the class which has public variables and define getter/setters for these public variables. so this way the public variables are not exposed and at the same time, the getters/setters deal with original public variables so don't have to worry about the change of values by the legacy or your code!
Avatar of bnrtech

ASKER

After much research, I have to agree.
>>After much research, I have to agree.

not really, you can use delegator pattern to achieve that!