Article Preview
Buy Now
| Print: | |
| PDF: |
Object-Oriented Thinking
Associating a Window to an Object
An Introduction to Weak References
Issue: 3.4 (March/April 2005)
Author: Charles Yeomans
Author Bio: Charles Yeomans is a software developer in Lexington, Kentucky.
Article Description: No description available.
Article Length (in bytes): 4,342
Starting Page Number: 33
RBD Number: 3414
Resource File(s): None
Related Link(s): None
Known Limitations: None
Excerpt of article text...
Consider the following example. You have a Mailbox class with a method ShowMessages that displays a window containing the list of messages. As long as this window is open, you would like the result of calling ShowMessages on a Mailbox to display the same window. At first glance, this is simple enough; the object holds a reference to the window and returns it when ShowMessages is called. The code might look something like the following.
Sub ShowMessages()
If me.pMsgWindow Is Nil then
me.pMsgWindow = new MessageListWindow
End if
me.pMsgWindow.Show
End Sub
However, there is a problem. When the window is closed, the Window object destroys the window. To show the window again, the object must create a new Window object. Thus the problem is how to know when the window is closed.
One solution would be to add a method WindowClosed. This method is called by the window in its Close event handler. Although this will certainly work, it has two disadvantages. First, we must add this special-purpose method to the class interface. Second, it requires that the window hold a reference to the object, thus creating a reference cycle. In this case, the second disadvantage is minor because the window can set its reference to Nil in its Close event. But the first disadvantage bothers me. Not only must I remember to call this method, it can be entirely too tempting to call such methods for other hackish purposes.
...End of Excerpt. Please purchase the magazine to read the full article.
Article copyrighted by REALbasic Developer magazine. All rights reserved.
|








