473,396 Members | 1,760 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,396 software developers and data experts.

Factory creation - when to destroy?

Subject is probably poorly worded, but bear with me as I describe the
situation that has me puzzled.

I create "Save" objects from a Save "factory" class. Many different
windows access this method to get their save object. I do this
because I am interested from the save factory when any of the many
windows uses that save object. I keep these save objects in a list in
the factory class that is iterated through when any one of them
saves.

My problem is that now, I need to know when it will no longer be used
by the window that requested the save object (the window is closed,
for example), so that I can remove it from the list and destroy it.

The only thing I could come up with is that in each window, on
closing, I would call a method on that save object that would let the
factory class know that I am done with it. But that seems like a bad
idea. I'm really not sure how else to make this happen. Is this just
poor design?

Aug 1 '07 #1
3 1530
Matt,

I personally don't think so. You are basically subscribing with the
call to the factory class, and when you are done, you need to unsubscribe.
A method here which does that seems reasonable in this case.

If you are sharing the instances between windows, then you will need to
reference count the objects as you hand them out to different windows (so
that when a window calls to unsubscribe, it doesn't wipe out the object that
another window is using, or at least, invalidate it in relation to your
factory).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Matt B" <ro**********@gmail.comwrote in message
news:11**********************@z24g2000prh.googlegr oups.com...
Subject is probably poorly worded, but bear with me as I describe the
situation that has me puzzled.

I create "Save" objects from a Save "factory" class. Many different
windows access this method to get their save object. I do this
because I am interested from the save factory when any of the many
windows uses that save object. I keep these save objects in a list in
the factory class that is iterated through when any one of them
saves.

My problem is that now, I need to know when it will no longer be used
by the window that requested the save object (the window is closed,
for example), so that I can remove it from the list and destroy it.

The only thing I could come up with is that in each window, on
closing, I would call a method on that save object that would let the
factory class know that I am done with it. But that seems like a bad
idea. I'm really not sure how else to make this happen. Is this just
poor design?

Aug 1 '07 #2
Matt B wrote:
[...]
My problem is that now, I need to know when it will no longer be used
by the window that requested the save object (the window is closed,
for example), so that I can remove it from the list and destroy it.

The only thing I could come up with is that in each window, on
closing, I would call a method on that save object that would let the
factory class know that I am done with it. But that seems like a bad
idea. I'm really not sure how else to make this happen. Is this just
poor design?
Not necessarily, I think.

How tied to the window (I assume by "window" you mean an instance of a
Form-based class) is the save object? Is it really specific exactly to
the window? If so, when you instantiate the save object, do you
actually have a reference to the window instance?

If all of those answers are "yes", then it seems to me it would be
reasonable for the factory or save object to subscribe a method to the
window's FormClosed event. When that event is raised, you would then
have the factory discard the save object.

If the instances are not so closely tied to each other, then yes...it
would probably be a bad idea to have them refer to each other that way.

So, it depends. But I don't see anything that clearly argues against
something like that.

Pete
Aug 1 '07 #3
Matt B wrote:
Subject is probably poorly worded, but bear with me as I describe the
situation that has me puzzled.

I create "Save" objects from a Save "factory" class. Many different
windows access this method to get their save object. I do this
because I am interested from the save factory when any of the many
windows uses that save object. I keep these save objects in a list in
the factory class that is iterated through when any one of them
saves.

My problem is that now, I need to know when it will no longer be used
by the window that requested the save object (the window is closed,
for example), so that I can remove it from the list and destroy it.

The only thing I could come up with is that in each window, on
closing, I would call a method on that save object that would let the
factory class know that I am done with it. But that seems like a bad
idea. I'm really not sure how else to make this happen. Is this just
poor design?
You can use the IDisposable interface when you want to control the life
time of an object. This is usually used when the life time of an object
needs to be controlled because it uses unmanaged resources, but usage
isn't limited to that.

As your object doesn't contain any unmanaged resources, you don't need a
Finalizer, which is often used when implementing IDisposable. As you
keep a list of the objects, they would never be finalised anyway.

Using IDisposable is the same thing that you are talking about, only you
call the method Dispose, and make the class inherit IDisposable.

One advantage is that you can use a using block to create and dispose
the class:

using (Save save = Save.Create()) {
... use the save object
}

This will automatically call the Dispose method at the end of the block.

--
Göran Andersson
_____
http://www.guffa.com
Aug 1 '07 #4

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

Similar topics

16
by: 4Space | last post by:
I've hit something of a snag. Problem: In a DSP application we have data profiles with a varying number of points. We have a number of Discrete Fourier transforms that are optimised for a...
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
1
by: Rajiv Das | last post by:
I have run into a design time problem and I look for help. This is the crux of the design. : class IShape{ public: virtual void Draw() = 0; }; class Circle : public IShape{
4
by: max | last post by:
Hello, I analyze this design pattern for a long time but I do not understand how this pattern work and what the purpose is? (I looked a this site...
2
by: Julia | last post by:
Hi, I have an application composed from layers like the following A --B ---C A is the top layer C uses an Abstract Factory to Create Concrete classes
8
by: Craig Buchanan | last post by:
I've seen design patterns for class factories that work well to create (fetch) objects, but I haven't seen anything about how to persist the class' data when it has changed. Is this done thru the...
4
by: anonymous.user0 | last post by:
Using the dotnet v1.1 framework (so no generics possible). I'd like to create a bunch of Factory classes that all inherit from a single abstract base Factory class. Each Factory is responsible...
10
by: sunny | last post by:
Does this following program implement the factory design.if not what are things that i have to change in order to make this following program to be designed to factory design pattern. ...
2
by: r035198x | last post by:
Ok i've overdone something here. I thought I'd make a factory of singletons during lunch package factory; import java.util.*; class SingletonFactory { static Hashtable<Class, Boolean>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.