473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Proper design pattern for event delegates outliving targets

I've been trying to think of the right way to design relationships
between objects with different desired lifetimes that raise events.
If an event source is a relatively permanent object and the event sink
is on an object that can come and go, what is the proper way to deal
with adding and removing the handler to the event list? Should the
event sink make sure to remove itself from the source's event list
before all other references to it are released? My problem is
determining a good way to disconnect the event sink from the event
source at an appropriate time. When some parent object creates a
child object, and the child object connects to an event source, then
the parent releases the child object, the only remaining reference to
the child object is the event source. So it stays alive (I'm
guessing) but will never get used again. What's the right way to get
rid of it? "Dispose" doesn't seem like the right answer because
that's supposed to be for un-managed resources and there's nothing
un-managed about this. Do I need to add a "Disconnect" method to
every child object that finds itself in this situation and explicitly
disconnect the child objects before I set the reference to null.
Should I make some sort of custom event delegate that uses a weak
reference so that the child object can die even though there is an
event handler connected to it?

Perhaps this is better explained with a specific example. I have a
wrapper class that pulls together a Direct3D Device and a collection
of textures and manages them as a unit. I have another class
"FrameCache" that manages a collection of "frames". A frame consists
of a reference to a texture, an index into the texture representing
cell from within it, and a matrix defining how that texture's cell is
drawn transformed on the screen. The frame cache is somewhat
temporary and there can be many frame caches connected to the same
display. There can also be many frame caches and many frames within a
cache that are connected to the same texture object. I'm thinking
that the frame cache will want to connect to the diplay device's reset
event so that it can force a re-load of the textures referenced by its
frames at that time. Why would the frame cache do this and not the
display you ask? Because only textures that are needed by some active
frame cache should be reloaded.

This is not the only example. I have event sinks all over my
application that are removing themselves from event sources' lists as
they are disposed because they have a shorter lifetime than the event
source. This is just the first time I've run into a case where I'm
not even dealing with a disposable object and I don't think I should
be. I'm thinking there must be another right answer.

So to summarize the questions involved:
1. Does a delegate / event source keep its "target" object alive (the
object that is the "container" of an event handler) even when there
are no other references to that target object? (I assume the answer to
this is yes)
2. In cases where the container/target object is referenced *only* by
such an event source, what is a good way to clean-up (good design),
assuming that the lifetime of the source is significantly longer than
that of the sink/target?
Jul 21 '05 #1
0 1353

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: bigoxygen | last post by:
Hi. I'm using a 3 tier FrontController Design for my web application right now. The problem is that I'm finding to have to duplicate a lot of code for similar functions; for example, listing...
0
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
1
by: Coder-X | last post by:
Hi, can someone give a real app example of where the Mediator design pattern could be used ( for instance in a relational database application )? Thanks, José Carlos Ferreira
1
by: R Reyes | last post by:
Hello All, I'm always looking for ways to improve my code. Most of the time (whenever I'm working on a project) I write a bunch of functions. Then after the project is finished, I put all the...
4
by: rawCoder | last post by:
Hi all, How Can You Raise Events Asynchronously ? Now for the details ... I want to do inter modular communication using events in such a way that the contributing modules need not...
0
by: Richard | last post by:
Hi there, Im using the Windows Media Player control on a windows form. When the media it is playing ends, i want to use the playstate event to notify the main MDI so that the Main MDI can close...
0
by: BlueMonkMN | last post by:
I've been trying to think of the right way to design relationships between objects with different desired lifetimes that raise events. If an event source is a relatively permanent object and the...
4
by: joh12005 | last post by:
Hello, i posted for suggestions a little idea even if it still needs further thoughts but as i'm sure you could help :) if would like to implement some kind of Condition class which i coud...
9
by: raylopez99 | last post by:
Here are two different ways of achieving a mediator pattern: the first, using circular references (for lack of a better way to describe it), but not using delegates, with the second using...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.