Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I just read the question what are callback frameworks?, where the asker cites the following from Effective Java:

The disadvantages of wrapper classes are few. One caveat is that wrapper classes are not suited for use in callback frameworks, wherein objects pass selfreferences to other objects for subsequent invocations (“callbacks”).

So I am curious, why are wrapper classes not suited for use in callback frameworks? And can you provide an example of what the problem is?

share|improve this question
    
Could you please explain the downvote? Reading the FAQ this seems like a perfect example of a questions that is on-topic! –  Bjarke Freund-Hansen Nov 3 '11 at 8:09

1 Answer 1

up vote 4 down vote accepted

The next line of Effective Java is helpful in clarifying the issue:

"Because a wrapped object doesn't know of its wrapper, it passes a reference to itself(this) and callbacks elude the wrapper. This is known as the SELF problem [Lieberman86]."

It seems like a trivial thing to make sure that all references to an object are via its wrapper, but wrappers are generally injected when a need arises and their design (their entire purpose of being) is to provide a low friction way to add more functionality to an existing class. This leads to subtle bugs, like the wrapper missing the event. Or the wrapper and wrapped object both registering for the same events - leading to duplicate processing and potential concurrency issues as well. If you don't know (don't have the source code) where callbacks are registered, it may be impossible to work around this problem. Additionally, wrappers are based on "pure" composition principles and callback frameworks force a "dirty" wrapper design.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.