|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
OOP: mutliple references to same Object: how?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 On Fri, 02 Mar 2007 18:04:01 +0800, Pieter
<pieterNOSPAMcoucke@hotmail.com> wrote: > [...] So what I somehow Well, to start with as long as you, instead of creating a new object, > 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? 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: Huh? What does any of the above have to do with NHibernate?> is this kind of technique usable with NHibernate? Pete 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 Sorry...I posted while trying to change that text. :)>> time: is this kind of technique usable with NHibernate? > > Huh? What does any of the above have to do with NHibernate? 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. "Peter Duniho" <NpOeStPe***@nnowslpianmk.com> wrote in message Thanks, and aren't such a caching tools available yet? It's impossible that 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. 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... :-) 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?
Show quote
"Pieter" <pieterNOSPAMcoucke@hotmail.com> wrote in message It is generally known as a Registry. You ask it for an object based on a 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? 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... :-) > 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 Surely implementations of this do exist in various libraries. However, > that I would be the first to want such a thing? 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 "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 My guess is that you are creating two instances of the same Company into two | 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? 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 "Joanna Carter [TeamB]" <joanna@not.for.spam> wrote in message Thanks a lot!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. And how can I now which are GCed? "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 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 > "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message Which would be actually horrible and very offensive, because I'm actually 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. Flemish, not Wallon ;-) On Mar 6, 2:24 pm, "Pieter" <pieterNOSPAMcou...@hotmail.com> wrote: Make Company class singleton or static, then all references will point> "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 ;-) to the same instances. |
|||||||||||||||||||||||