BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Can two separate instances of a class share the same instanc

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design
View previous topic :: View next topic  
Author Message
Abdullah Kauchali
Guest





PostPosted: Fri Feb 04, 2005 7:55 am    Post subject: Can two separate instances of a class share the same instanc Reply with quote



Is it possible to create two instances of descendant classes
but have them both share one instance of a parent class?

I'm obviously missing something basic here.


TParent
Quote:

TChild



child1 := TChild.Create();
child2 := TChild.Create(); //<-- creates another instance of TParent

TParent *cannot* be a singleton. We can have many instances of TParent.

Is it somehow possible to pass a pre-instantiated TParent into the
create method of TChild so that it uses that as its ancestor instead of
creating a new one?

Thanks is advance & kind regards,

Abdullah

Back to top
Joanna Carter (TeamB)
Guest





PostPosted: Fri Feb 04, 2005 8:34 am    Post subject: Re: Can two separate instances of a class share the same ins Reply with quote



"Abdullah Kauchali" <non (AT) non (DOT) com> a écrit dans le message de news:
42032a61$1 (AT) newsgroups (DOT) borland.com...

Quote:
Is it possible to create two instances of descendant classes
but have them both share one instance of a parent class?

Don't create the Parent inside the Child, create it first and then pass it
to the child constructor.

parent := TParent.Create;

child1 := TChild.Create(parent);
child2 := TChild.Create(parent);

Joanna

--
Joanna Carter (TeamB)

Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker



Back to top
Abdullah Kauchali
Guest





PostPosted: Fri Feb 04, 2005 9:00 am    Post subject: Re: Can two separate instances of a class share the same ins Reply with quote



Joanna Carter (TeamB) wrote:
Quote:
Don't create the Parent inside the Child, create it first and then pass it
to the child constructor.

parent := TParent.Create;

child1 := TChild.Create(parent);
child2 := TChild.Create(parent);


Hi Joanna, thanks for assisting ...

Won't the inheritance structure automatically create a new instance of
Parent each time TChild.create is executed?

Regards

Abdullah

.... ps. I am posting the "real" question ... what I'd like to model via
proper Class structures ... my OO knowledge is poor.


Back to top
John Leavey
Guest





PostPosted: Fri Feb 04, 2005 9:05 am    Post subject: Re: Can two separate instances of a class share the same ins Reply with quote

Abdullah Kauchali wrote:

Quote:
Is it possible to create two instances of descendant classes
but have them both share one instance of a parent class?

I'm obviously missing something basic here.


TParent
|
TChild


child1 := TChild.Create();
child2 := TChild.Create(); //<-- creates another instance of TParent

TParent cannot be a singleton. We can have many instances of TParent.

Is it somehow possible to pass a pre-instantiated TParent into the
create method of TChild so that it uses that as its ancestor instead
of creating a new one?

Thanks is advance & kind regards,

Abdullah


Either :-

Make the contructor of TChild take a parameter of type TParent so that
the appropriate Parent value is assigned to an internal field of the
new TChild instance

TParent = Class;

TChild = Class
private
FParent;
public
constructor Create( P: TParent );
end;

...

constructor TChild.Create( P: TParent );
begin
inherited Create;
FParent := P
end;

or

Add a CreateChild() function to the TParent class that returns a new
instance of TChild - this routine assigns Self to the Parent field of
the new TChild instance

TParent = Class;

TChild = Class;
private
FParent: TParent;
end;

TParent = Class
public
function CreateChild: TChild;
end;

...

function TParent.CreateChild;
begin
Result := TChild.Create;
Result.FParent := Self;
end;



John Leavey

Back to top
Abdullah Kauchali
Guest





PostPosted: Fri Feb 04, 2005 9:07 am    Post subject: Re: Can two separate instances of a class share the same ins Reply with quote

Thanks John,

Let me chew on this please ... gimme a moment to construct the example.

.... I'll be back ... Smile
Back to top
Joanna Carter (TeamB)
Guest





PostPosted: Fri Feb 04, 2005 9:13 am    Post subject: Re: Can two separate instances of a class share the same ins Reply with quote

"Abdullah Kauchali" <non (AT) non (DOT) com> a écrit dans le message de news:
[email]42033a02 (AT) newsgroups (DOT) borland.com[/email]...

Quote:
Won't the inheritance structure automatically create a new instance of
Parent each time TChild.create is executed?

Ah, now I see that you mean Parent as being the ancestor class.

No, you cannot separate the instantiation of the child class from the
instantiation of the parent class. The child class is said to have an 'is a'
relationship; using parent and child is not a good example for this
explanation, so let me use an anlogy here :

Person
^
Quote:

Student


A Student 'is a' Person; you cannot have two Students who are the same
Person.

When we use the terms Parent and Child, we usually use that to refer to the
'has a' concept of Aggregation or Composition, where one object either
contains or owns other objects.

Is this the concept you are trying to design ?

Joanna

--
Joanna Carter (TeamB)

Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker



Back to top
Joanna Carter (TeamB)
Guest





PostPosted: Fri Feb 04, 2005 9:17 am    Post subject: Re: Can two separate instances of a class share the same ins Reply with quote

"John Leavey" <johnl@[no_spam].compufile.co.uk> a écrit dans le message de
news: [email]42033aee (AT) newsgroups (DOT) borland.com[/email]...

Quote:
Make the contructor of TChild take a parameter of type TParent so that
the appropriate Parent value is assigned to an internal field of the
new TChild instance

Nice try John, but Abdullah has already replied that he is trying to
separate the instantiation of the ancestor (parent) class from the derived
(child) class.

I have replied, asking for clarification of his situation; please add your
comments as the discussion progresses.

Joanna

--
Joanna Carter (TeamB)

Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker



Back to top
Abdullah Kauchali
Guest





PostPosted: Fri Feb 04, 2005 9:20 am    Post subject: Re: Can two separate instances of a class share the same ins Reply with quote

Joanna Carter (TeamB) wrote:

Quote:
When we use the terms Parent and Child, we usually use that to refer to the
'has a' concept of Aggregation or Composition, where one object either
contains or owns other objects.

Beautiful. I am seeing a speck of light already! Smile ...


Quote:
Is this the concept you are trying to design ?

Yes! How do we go about aggregating the data (fields) of
different instances of an ancestor class so that they are
one and only one version of the truth for multiple instances
of the descendants? (I hope I phrased the question
correctly this time!)

Back to top
Joanna Carter (TeamB)
Guest





PostPosted: Fri Feb 04, 2005 10:48 am    Post subject: Re: Can two separate instances of a class share the same ins Reply with quote

"Abdullah Kauchali" <non (AT) non (DOT) com> a écrit dans le message de news:
[email]42033e7a (AT) newsgroups (DOT) borland.com[/email]...

Quote:
Yes! How do we go about aggregating the data (fields) of
different instances of an ancestor class so that they are
one and only one version of the truth for multiple instances
of the descendants? (I hope I phrased the question
correctly this time!)

Not quite. We will have to take this a step at a time Smile)

Aggregation and Composition have nothing to do with Inheritance.

You talk about ancestors and descendants, but these are terms that are only
used in inheritance.

Let me define more strongly Aggregation and Composition :

Aggregation
- One object contains a list of other objects.
- The contained objects can exist without being part of the containing
object
- example : Car -> Wheel - you can take the Wheels and put them on another
Car before destroying the original Car

Composition
- One object contains a list of other objects
- The contained object has no context outside of the containing object
- example : Invoice -> Invoice Line - an Invoice Line can only belong to one
Invoice, if you delete the invoice, the lines go with it.

Does your model fit into either of these ? If not then could you give real
names to the classes you are trying to model ?

Joanna

--
Joanna Carter (TeamB)

Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker



Back to top
Rudy Velthuis [TeamB]
Guest





PostPosted: Fri Feb 04, 2005 12:08 pm    Post subject: Re: Can two separate instances of a class share the same ins Reply with quote

Abdullah Kauchali wrote:

Quote:
Won't the inheritance structure automatically create a new instance of
Parent each time TChild.create is executed?

Why?

--
Rudy Velthuis [TeamB] http://rvelthuis.bei.t-online.de

"I think there is a world market for maybe five computers."
-- Thomas Watson (1874-1956), Chairman of IBM, 1943

Back to top
Abdullah Kauchali
Guest





PostPosted: Fri Feb 04, 2005 12:10 pm    Post subject: Re: Can two separate instances of a class share the same ins Reply with quote

Rudy Velthuis [TeamB] wrote:
Quote:
Abdullah Kauchali wrote:


Won't the inheritance structure automatically create a new instance of
Parent each time TChild.create is executed?


Why?


Bad terminology ... Joanna corrected me. It was supposed to be phrased
thusly:

Won't the inheritance structure automatically create a new instance of
the *ancestor* each time the *descendant* is created?

Back to top
Rudy Velthuis [TeamB]
Guest





PostPosted: Fri Feb 04, 2005 12:15 pm    Post subject: Re: Can two separate instances of a class share the same ins Reply with quote

Abdullah Kauchali wrote:

Quote:
Bad terminology ... Joanna corrected me. It was supposed to be
phrased thusly:

Won't the inheritance structure automatically create a new instance of
the ancestor each time the descendant is created?

You mean as part of the descendant instance? Yes, of course. To avoid
this, you should not use inheritance.

--
Rudy Velthuis [TeamB] http://rvelthuis.bei.t-online.de

"Some cause happiness wherever they go; others, whenever they go."
- Oscar Wilde (1854-1900)

Back to top
Abdullah Kauchali
Guest





PostPosted: Fri Feb 04, 2005 12:18 pm    Post subject: Re: Can two separate instances of a class share the same ins Reply with quote

Rudy Velthuis [TeamB] wrote:

Quote:
You mean as part of the descendant instance? Yes, of course. To avoid
this, you should not use inheritance.

Correcto! :)


Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.