OOP: mutliple references to same Object: how?

P

Pieter

Hi,

In my object oriented application (VB.NET 2.0, Windows Forms), a lot of
objects are opened in different forms by a user. For instance (a stupid
exemple, but it shows the easiest what's happening):
-> two instances of the Company-Object: MyCompany1 and MyCompany2, both of
them are poiting to the same Company: for isntance the Company Microsoft.

The problem is: if the user makes changes to one of the 2 instances, these
changes are not automaticly made to the other instance. So what I somehow
would need is a way that there is some local cache of the opened Business
Objects, and in one is asked that is already opened, that a reference is
given to the same object.

This must definetly be something that is a common practice in OOP, but I
can't find out how to do this?

And a secondary question: As we will start using Hibernate in some time: is
this kind of technique usable with NHibernate?

Thanks a lot in advance,

Pieter
 
P

Peter Duniho

[...] So what I somehow
would need is a way that there is some local cache of the opened Business
Objects, and in one is asked that is already opened, that a reference is
given to the same object.

This must definetly be something that is a common practice in OOP, but I
can't find out how to do this?

Well, to start with as long as you, instead of creating a new object,
simply copy an existing reference, then that's all you need to do to
create the situation you're asking about.

Now, how to make sure that happens depends on the situation. But
generally speaking, when you try to instantiate a new instance of an
object, the first thing you would do is check to see if you already have
one that meets the criteria you have.

In your example, presumably you would use "Microsoft" as the key to
instantiating an object. In that case, you maintain a list of all the
objects you've already instantiated and search the list before you
instantiate a new object. So when creating a new object of key
"Microsoft", you first look in your list of existing objects to see if one
labeled "Microsoft" is already there. If it is, rather than creating a
new object, you just return the reference to the existing one.

The exact implementation will vary according to your needs. But that's
the basic idea. You'll note that you need to implement the caching
mechanism yourself. There is no uniform "local cache" of objects to
handle this for you.
And a secondary question: As we will start using Hibernate in some time:
is this kind of technique usable with NHibernate?

Huh? What does any of the above have to do with NHibernate?

Pete
 
P

Peter Duniho

Huh? What does any of the above have to do with NHibernate?

Sorry...I posted while trying to change that text. :)

Anyway, what I should have said is simply that since you have to implement
the object cache yourself, it is persisted just as any of your data would
be persisted under NHibernate.
 
P

Pieter

Peter Duniho said:
On Fri, 02 Mar 2007 18:04:01 +0800, Pieter

The exact implementation will vary according to your needs. But that's
the basic idea. You'll note that you need to implement the caching
mechanism yourself. There is no uniform "local cache" of objects to
handle this for you.

Thanks, and aren't such a caching tools available yet? It's impossible that
I would be the first to want such a thing?
Huh? What does any of the above have to do with NHibernate?

Because of the fact that NHibernate is an ORM, if I would have written
NHibernate, it would have such a functionality implemented... :)
 
J

Joanna Carter [TeamB]

"Pieter" <[email protected]> a écrit dans le message de (e-mail address removed)...

| In my object oriented application (VB.NET 2.0, Windows Forms), a lot of
| objects are opened in different forms by a user. For instance (a stupid
| exemple, but it shows the easiest what's happening):
| -> two instances of the Company-Object: MyCompany1 and MyCompany2, both of
| them are poiting to the same Company: for isntance the Company Microsoft.
|
| The problem is: if the user makes changes to one of the 2 instances, these
| changes are not automaticly made to the other instance. So what I somehow
| would need is a way that there is some local cache of the opened Business
| Objects, and in one is asked that is already opened, that a reference is
| given to the same object.
|
| This must definetly be something that is a common practice in OOP, but I
| can't find out how to do this?

My guess is that you are creating two instances of the same Company into two
separate references :

Company myCompany1 = new Company(...);

Company myCompany2 = new Company(...);

What you really want is to have two references to the same instance. This is
possibly easiest achieved by using a Class Factory rather than the
constructors of the classes, and is something that a lot of persistence
mechanisms use.

public static class CompanyFactory
{
private static Dictionary<int, WeakReference> instances = new
Dictionary<int, WeakReference>();

public static Company CreateCompany(int id)
{
if (instances.ContainsKey(id)
return (Company) instances[id].Target; // this will resurrect the
target if necessary

Company newCompany = new Company(id);
// populate new Company
WeakReference newInstance = new WeakReference(newCompany, true);
instances.Add(id, newInstance);
return newCompany;
}
}

Then this static method can be called from more than one place and will
always return the same instance. You also need to set up a strategy for
periodically sweeping this list and removing all WeakReferences whose
targets have been GCed.

Joanna
 
P

Pieter

Joanna Carter said:
Then this static method can be called from more than one place and will
always return the same instance. You also need to set up a strategy for
periodically sweeping this list and removing all WeakReferences whose
targets have been GCed.

Thanks a lot!
And how can I now which are GCed?
 
J

Joanna Carter [TeamB]

"Pieter" <[email protected]> a écrit dans le message de (e-mail address removed)...

| And how can I now which are GCed?

Check the target for null or the WeakReference's IsAlive property, depending
if you have a long or short reference. See the help for WeakReference for
more ideas :)

Joanna
 
C

Cor Ligthert [MVP]

Pieter,

They should change in both, however be aware that sometimes people including
me do this..


Private Pieter as new Belg

Private sub EU
dim Pieter as new Walon
End Sub

Now that although Pieter is created as Belg globaly, when it is used inside
the Eu he will the threaten as walon.

Cor


"Pieter" <pieterNO

(e-mail address removed)> schreef in bericht
news:[email protected]...
 
P

PS

Pieter said:
Thanks, and aren't such a caching tools available yet? It's impossible
that I would be the first to want such a thing?

It is generally known as a Registry. You ask it for an object based on a
key. If it already has it it returns the (same) instance to you. If it does
not then it deelgates the construction to a factory / builder and returns
this object.

PS
 
P

Peter Duniho

Thanks, and aren't such a caching tools available yet? It's impossible
that I would be the first to want such a thing?

Surely implementations of this do exist in various libraries. However,
that may or may not be required. In the simplest case, coding it yourself
is relatively trivial.

There are, of course, a wide variety of added optional features one might
include in such a caching system. If you desire more behavior than just
that which you've asked about, it might be worthwhile to explore existing
libraries. But if all you need is to maintain a list of objects and
inspect the list before creating a new instance of an object, returning a
previously-created object if some specific data matches, I'm not sure I
see the need for an external library.

Pete
 
P

Pieter

Cor Ligthert said:
Now that although Pieter is created as Belg globaly, when it is used
inside the Eu he will the threaten as walon.

Which would be actually horrible and very offensive, because I'm actually
Flemish, not Wallon ;-)
 
O

oscar.acostamontesde

Which would be actually horrible and very offensive, because I'm actually
Flemish, not Wallon ;-)

Make Company class singleton or static, then all references will point
to the same instances.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

OOP question 5
OOP Design Question 7
FSM in OOP 6
Basic OOP Collections Question 2
OOP vb.net 7
OOP Inheritance 8
OOP object instance assignment in sub new() 11
Multiple instances of same object ? 6

Top