views:

528

answers:

3

I'm looking for something similar to the Proxy pattern or the Dynamic Proxy Classes, 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 the Proxy class in Action Script 3 would be fine.

Here's what I want to achieve in general:

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.

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.

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.


Edit: 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?

+1  A: 

The design pattern you are looking for is: Differential Execution. I do believe.

How does differential execution work?

Is a question I answered that deals with this.

However, may I suggest that you use a callback instead? You will have to read about this, but the general idea is that you can implement interfaces (often called listeners) that active upon "something interesting" happening. Such as having a data structure be changed.

Obligitory links:

Wiki Differential execution

Wiki Callback

Alright, here is the answer as I see it. Differential Execution is O(N) time. This is really reasonable, but if that doesn't work for ya Callbacks will. Callbacks basically work by passing a method by parameter to your class that is changing the array. This method will take the value changed and the location of the item, pass it back by parameter to the "storage class" and change the value approipriately. So, yes, you have to back each change with a method call.

I realize now this is not what you want. What it appears that you want is a way that you can supply some kind of listener on each variable in an array that would be called when that item is changed. The listener would then change the corresponding array in your "backup" to refect this change.

Natively I can't think of a way to do this. You can, of course, create your own listeners and events, using an interface. This is basically the same idea as the callbacks, though nicer to look at.

Then there is reflection... Java has reflection, and I am positive you can write something using it to do this. However, reflection is notoriously slow. Not to mention a pain to code (in my opinion).

Hope that helps...

windfinder
That's interesting material, but not quite what I need, though. The Differential Execution is too slow for my purpose (because of the repeated scan-compare), and I also fear threading issues. The callback would help only if there was sth triggering the proper event upon data change.
Hanno Fietz
I am sorry? Why won't callback work? I do a lot of delta generation stuff myself, and its almost always done by callback.
windfinder
Maybe I didn't understand, but who calls the callback? I. e., I have a thread that changes a data structure and another that wants to react upon that. Do I have to put in a function call behind every change, or can sth detect the change and run the callback, like an onChange event handler? How?
Hanno Fietz
Thanks for all the effort. I know a lot more now, though I haven't decided on what to do.
Hanno Fietz
+1  A: 

If with "properties" you mean JavaBean properties, i.e. represented bay a getter and/or a setter method, then you can use a dynamic proxy to intercept the set method.

If you mean instance variables, then no can do - not on the Java level. Perhaps something could be done by manipulations on the byte code level though.

Actually, the easiest way to do it is probably by using AspectJ and defining a set() pointcut (which will intercept the field access on the byte code level).

Michael Borgwardt
Huh, I'd rather not go down that deep. But thanks anyway.
Hanno Fietz
AspectJ is really the easiest way to do it - it's just not pure Java anymore.
Michael Borgwardt
A: 

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

So in fact, the objects you want to monitor are no convenient beans but a resurgence of C structs. The only way that comes to my mind to do that is with the Field Access call in JVMTI.

Damien B