Can two separate instances of a class share the same instance of a Parent class?

Can two separate instances of a class share the same instance of a Parent class?

Post by Abdullah K » Sat, 05 Feb 2005 16:55:13


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
 
 
 

Can two separate instances of a class share the same instance of a Parent class?

Post by Team » Sat, 05 Feb 2005 17:34:04


42032a61$ XXXX@XXXXX.COM ...


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

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

 
 
 

Can two separate instances of a class share the same instance of a Parent class?

Post by Abdullah K » Sat, 05 Feb 2005 18:00:55


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.
 
 
 

Can two separate instances of a class share the same instance of a Parent class?

Post by John Leave » Sat, 05 Feb 2005 18:05:50


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
 
 
 

Can two separate instances of a class share the same instance of a Parent class?

Post by Abdullah K » Sat, 05 Feb 2005 18:07:59

Thanks John,

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

... I'll be back ... :)
 
 
 

Can two separate instances of a class share the same instance of a Parent class?

Post by Team » Sat, 05 Feb 2005 18:13:58


XXXX@XXXXX.COM ...


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

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

Can two separate instances of a class share the same instance of a Parent class?

Post by Team » Sat, 05 Feb 2005 18:17:05

"John Leavey" <johnl@[no_spam].compufile.co.uk> a rit dans le message de



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

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

Can two separate instances of a class share the same instance of a Parent class?

Post by Abdullah K » Sat, 05 Feb 2005 18:20:57


Beautiful. I am seeing a speck of light already! :) ...



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!)
 
 
 

Can two separate instances of a class share the same instance of a Parent class?

Post by Team » Sat, 05 Feb 2005 19:48:38


XXXX@XXXXX.COM ...


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

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

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

Can two separate instances of a class share the same instance of a Parent class?

Post by Rudy Velth » Sat, 05 Feb 2005 21:08:05


Why?

--
Rudy Velthuis [TeamB] http://www.yqcomputer.com/

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

Can two separate instances of a class share the same instance of a Parent class?

Post by Abdullah K » Sat, 05 Feb 2005 21:10:20


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?
 
 
 

Can two separate instances of a class share the same instance of a Parent class?

Post by Rudy Velth » Sat, 05 Feb 2005 21:15:37


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

--
Rudy Velthuis [TeamB] http://www.yqcomputer.com/

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

Can two separate instances of a class share the same instance of a Parent class?

Post by Abdullah K » Sat, 05 Feb 2005 21:18:19


Correcto! :)