Note that there are some explanatory texts on larger screens.

plurals
  1. POIn Java, how can I construct a "proxy wrapper" around an object which invokes a method upon changing a property?
    text
    copied!<p>I'm looking for something similar to the <a href="http://en.wikipedia.org/wiki/Proxy_pattern" rel="nofollow noreferrer">Proxy pattern</a> or the <a href="http://java.sun.com/j2se/1.3/docs/guide/reflection/proxy.html" rel="nofollow noreferrer">Dynamic Proxy Classes</a>, only that I don't want to intercept method calls before they are invoked on the real object, but rather I'd like to intercept properties that are being changed. I'd like the proxy to be able to represent multiple objects with different sets of properties. Something like <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/Proxy.html" rel="nofollow noreferrer">the Proxy class in Action Script 3</a> would be fine.</p> <p>Here's what I want to achieve in general:</p> <p>I have a thread running with an object that manages a list of values (numbers, strings, objects) which were handed over by other threads in the program, so the class can take care of creating regular persistent snapshots on disk for the purpose of checkpointing the application. This persistor object manages a "dirty" flag that signifies whether the list of values has changed since the last checkpoint and needs to lock the list while it's busy writing it to disk.</p> <p>The persistor and the other components identify a particular item via a common name, so that when recovering from a crash, the other components can first check if the persistor has their latest copy saved and continue working where they left off.</p> <p>During normal operation, in order to work with the objects they handed over to the persistor, I want them to receive a reference to a proxy object that looks as if it were the original one, but whenever they change some value on it, the persistor notices and acts accordingly, for example by marking the item or the list as dirty before actually setting the real value.</p> <hr> <p><strong>Edit</strong>: Alternatively, are there generic setters (like in PHP 5) in Java, that is, a method that gets called if a property doesn't exist? Or is there a type of object that I can add properties to at runtime?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload