On Sunday 22 July 2007 08:56, Pit C. wrote:
2007/7/21, Jesse M. [email protected]:
I’m trying to use SimpleDelegator, but am having a problem overring a method.
(… example: B delegates to A, A should call back to B …)
Is there a good way to do this? Since I’m
changing the delegated object with setobj, subclassing won’t work, and I’d
rather not have to override all methods that call overridden methods.
Jesse, can you tell us a bit more of what you are trying to achieve?
Sure. Basically, I have one class that wraps an external process (and
provides
other goodies). It has a #puts method to send input to that process, and
receive back parsed output. #puts is called both on its own from
external code,
and from within other methods of the same class. The problem then was
that the
external process ate more and more memory over time (and no, this
optimization
is not premature. Its a definite problem). The solution that came to my
mind
was to create a delegating class that counts how many times #puts has
been
called, and restarts the process by killing the old wrapper object and
creating
a new one (in my app, the calls to #puts were independent of each other,
so I
could get away with this). So I overrode #puts, but the calls to #puts
in the
original class are not using it. I could put killing/restarting code
into the
original class, but I think separating it would be a nicer solution.
For example, in the lifetime of a B instance, how often do you change
the delegate?
At regular intervals (every X calls to #puts, as above). In one specific
instance, that amounted to around every half hour over a 12 hour period.
Do you want the set of delegated methods to change when
changing the delegate, or is there a fixed set of methods to be
delegated from B to the delegate?
I want all calls to #puts, whether from the delagating or delegated
class, to
go through my overridden version (but still have the original accessible
via
super() or something).
Can you change the source code of the delegate?
Yeah, but I’d rather not.
Do you need to create instances of the delegate classes independent of B?
Yes.