Home All Groups Group Topic Archive Search About

OOP: mutliple references to same Object: how?

Author
2 Mar 2007 10:04 AM
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

Author
2 Mar 2007 10:14 AM
Peter Duniho
On Fri, 02 Mar 2007 18:04:01 +0800, Pieter 
<pieterNOSPAMcoucke@hotmail.com> wrote:

> [...] 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
Author
2 Mar 2007 10:18 AM
Peter Duniho
On Fri, 02 Mar 2007 18:14:56 +0800, Peter Duniho 
<NpOeStPe***@nnowslpianmk.com> wrote:

>> 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?

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.
Author
2 Mar 2007 10:19 AM
Pieter
"Peter Duniho" <NpOeStPe***@nnowslpianmk.com> wrote in message
news:op.toj3260t8jd0ej@petes-computer.local...
> On Fri, 02 Mar 2007 18:04:01 +0800, Pieter
> <pieterNOSPAMcoucke@hotmail.com> wrote:
>
> 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... :-)
Author
2 Mar 2007 2:08 PM
Ornette
Hello,

Look at the Singleton Pattern :
http://en.wikipedia.org/wiki/Singleton_pattern
See also design patterns on Microsoft' :
http://msdn.microsoft.com/practices/topics/patterns/

Ornette.


Show quote
"Pieter" <pieterNOSPAMcoucke@hotmail.com> a écrit dans le message de
news:OMMHGRLXHHA.480@TK2MSFTNGP02.phx.gbl...
> "Peter Duniho" <NpOeStPe***@nnowslpianmk.com> wrote in message
> news:op.toj3260t8jd0ej@petes-computer.local...
>> On Fri, 02 Mar 2007 18:04:01 +0800, Pieter
>> <pieterNOSPAMcoucke@hotmail.com> wrote:

> Thanks, and aren't such a caching tools available yet? It's impossible
> that I would be the first to want such a thing?
Author
2 Mar 2007 7:55 PM
PS
Show quote
"Pieter" <pieterNOSPAMcoucke@hotmail.com> wrote in message
news:OMMHGRLXHHA.480@TK2MSFTNGP02.phx.gbl...
> "Peter Duniho" <NpOeStPe***@nnowslpianmk.com> wrote in message
> news:op.toj3260t8jd0ej@petes-computer.local...
>> On Fri, 02 Mar 2007 18:04:01 +0800, Pieter
>> <pieterNOSPAMcoucke@hotmail.com> wrote:
>>
>> 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?

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
Show quote
>
>> 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... :-)
>
Author
3 Mar 2007 9:06 AM
Peter Duniho
On Fri, 02 Mar 2007 18:19:31 +0800, Pieter 
<pieterNOSPAMcoucke@hotmail.com> wrote:

> 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
Author
2 Mar 2007 10:50 AM
Joanna Carter [TeamB]
"Pieter" <pieterNOSPAMcoucke@hotmail.com> a écrit dans le message de news:
O9L1cILXHHA.4***@TK2MSFTNGP06.phx.gbl...

Show quote
| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Author
2 Mar 2007 1:21 PM
Pieter
"Joanna Carter [TeamB]" <joanna@not.for.spam> wrote in message
news:u2igpiLXHHA.4132@TK2MSFTNGP06.phx.gbl...
> 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?
Author
2 Mar 2007 5:20 PM
Joanna Carter [TeamB]
"Pieter" <pieterNOSPAMcoucke@hotmail.com> a écrit dans le message de news:
e1h3s2MXHHA.5***@TK2MSFTNGP06.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Author
2 Mar 2007 6:58 PM
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

SPAMcoucke@hotmail.com> schreef in bericht
Show quote
news:O9L1cILXHHA.4188@TK2MSFTNGP06.phx.gbl...
> 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
>
Author
6 Mar 2007 1:24 PM
Pieter
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:eMFEqxPXHHA.1396@TK2MSFTNGP05.phx.gbl...
> 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 ;-)
Author
7 Mar 2007 1:33 PM
oscar.acostamontesde@googlemail.com
On Mar 6, 2:24 pm, "Pieter" <pieterNOSPAMcou...@hotmail.com> wrote:
> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in messagenews:eMFEqxPXHHA.1***@TK2MSFTNGP05.phx.gbl...
>
> > 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 ;-)

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

AddThis Social Bookmark Button