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

OOP: mutliple references to same Object: how?

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
Mar 2 '07 #1
12 1123
On Fri, 02 Mar 2007 18:04:01 +0800, Pieter
<pi****************@hotmail.comwrote:
[...] 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
Mar 2 '07 #2
On Fri, 02 Mar 2007 18:14:56 +0800, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
>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.
Mar 2 '07 #3
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 02 Mar 2007 18:04:01 +0800, Pieter
<pi****************@hotmail.comwrote:

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... :-)
Mar 2 '07 #4
"Pieter" <pi****************@hotmail.coma écrit dans le message de news:
O9**************@TK2MSFTNGP06.phx.gbl...

| 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, WeakReferenceinstances = 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
Mar 2 '07 #5
"Joanna Carter [TeamB]" <jo****@not.for.spamwrote in message
news:u2**************@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?
Mar 2 '07 #6
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.
"Pieter" <pi****************@hotmail.coma écrit dans le message de
news:OM*************@TK2MSFTNGP02.phx.gbl...
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
>On Fri, 02 Mar 2007 18:04:01 +0800, Pieter
<pi****************@hotmail.comwrote:
Thanks, and aren't such a caching tools available yet? It's impossible
that I would be the first to want such a thing?
Mar 2 '07 #7
"Pieter" <pi****************@hotmail.coma écrit dans le message de news:
e1**************@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
Mar 2 '07 #8
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

SP********@hotmail.comschreef in bericht
news:O9**************@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

Mar 2 '07 #9
PS

"Pieter" <pi****************@hotmail.comwrote in message
news:OM*************@TK2MSFTNGP02.phx.gbl...
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
>On Fri, 02 Mar 2007 18:04:01 +0800, Pieter
<pi****************@hotmail.comwrote:

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
>
>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... :-)

Mar 2 '07 #10
On Fri, 02 Mar 2007 18:19:31 +0800, Pieter
<pi****************@hotmail.comwrote:
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
Mar 3 '07 #11
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:eM**************@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 ;-)
Mar 6 '07 #12
On Mar 6, 2:24 pm, "Pieter" <pieterNOSPAMcou...@hotmail.comwrote:
"Cor Ligthert [MVP]" <notmyfirstn...@planet.nlwrote in messagenews:eM**************@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.

Mar 7 '07 #13

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

Similar topics

8
by: Berislav Lopac | last post by:
I have a some experience in OOP, primarily with Java and JavaScript, and a little with PHP4. I have just checked some texts about upcoming PHP5 features, and particularly the OOP ones, and there is...
56
by: Xah Lee | last post by:
What are OOP's Jargons and Complexities Xah Lee, 20050128 The Rise of Classes, Methods, Objects In computer languages, often a function definition looks like this: subroutine f (x1, x2, ...)...
77
by: nospam | last post by:
Reasons for a 3-tier achitecture for the WEB? (NOTE: I said, WEB, NOT WINDOWS. DON'T shoot your mouth off if you don't understand the difference.) I hear only one reason and that's to switch a...
51
by: nospam | last post by:
THIS IS the DOTNETJUNKIES MESSAGE ------------------------- We're Sorry As many of you know we have recently launched SqlJunkies.com. We have overhauled our runtime and will be using it on...
5
by: Thomas Jespersen | last post by:
Hello I've been reading a lot of great OOD/OOP books lately (e.g.. Martin Fowlers UML Distilled, Patterns of Enterprise Application Architecture, Refactoring, Kent Becks's Test Driven...
16
by: Manuel | last post by:
Please, excuse me for starting this discussion. I'm not a troll, and I love the OOP architecture. However I'm a C++ newbie, and sometimes it's hard for me to fully understand the improvement of...
4
by: Nemisis | last post by:
Hi everyone, I have 2 classes, Company and Contact, a company can have 1 or more contacts. A contact can only be in one company. I have created a Company class object that contains all the...
12
by: Pieter | last post by:
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...
18
by: dancer | last post by:
I haven't given up yet, but I'm wondering if OOP is worth the effort. I know that almost everybody in this forum thinks OOP is the only way to go, but is there anybody out there who believes...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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,...

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.