Parent and Child things again.

For questions and postings not covered by the other forums
ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Parent and Child things again.

Postby ConvertFromOldNGs » Fri Aug 07, 2009 11:50 am

by JADE Kid - Ray Hidayat >> Tue, 11 Apr 2000 23:31:15 GMT

If I have a multiple things as parent references, ie a collection
And I have one child reference, will the child be deleted when all the parents are gone, or just one of them is deleted?

--
Ray Hidayat
JADE Kid - 2000
www.jadekids.com

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Parent and Child things again.

Postby ConvertFromOldNGs » Fri Aug 07, 2009 11:50 am

by Dean Cooper >> Tue, 11 Apr 2000 23:52:21 GMT
If I have a multiple things as parent references, ie a collection And I have one child reference, will the child be deleted when
all the parents are gone, or just one of them is deleted?

When a parent object is deleted, all child objects referenced from that parent are deleted, regardless of whether or not they referenced as children from other parent objects.

For example, say you built something like this:

Company :: allEmployees (parent) inverse Employee :: myCompany (child)

Dept :: allEmployees (parent) inverse Employee :: myDept (child)

Say you have one Company object, one Dept object and one Employee object. The myCompany reference of the Employee references the Company object (so the Employee is in the Company's allEmployees collection), and the myDept reference of the Employee references the Dept object (so the Employee is in the Dept's allEmployees collection).

If you delete the Company object, the Employee object will be deleted (because it is a child of the Company), and due to the deletion of the Employee, it will be removed from the allEmployees collection on the Dept. So after the operation, the only object left is the Dept, with an empty allEmployees collection.

Dean.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Parent and Child things again.

Postby ConvertFromOldNGs » Fri Aug 07, 2009 11:50 am

by JADE Kid - Ray Hidayat >> Wed, 12 Apr 2000 6:38:39 GMT

I meant that it was in this design:

Parent Player
myChannel
|
Child Channel
allPeople

If you don't know, a channel is a chatroom, and it can have many players in it, but I want the channel to disappear once all the players disappear.

Now, does it wait for all the players to die, or does it wait for one player to die?

--
Ray Hidayat
JADE Kid - 2000
www.jadekids.com

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Parent and Child things again.

Postby ConvertFromOldNGs » Fri Aug 07, 2009 11:50 am

by Dean Cooper >> Wed, 12 Apr 2000 7:26:56 GMT
I meant that it was in this design:

Parent Player
myChannel
|
Child Channel
allPeople

If you don't know, a channel is a chatroom, and it can have many players in it, but I want the channel to disappear once all the players disappear.

Now, does it wait for all the players to die, or does it wait for one player to die?

With Player as the parent, when you delete a Player object, the Channel object to which the Player is related will be deleted as well, regardless of whether or not there are any other Players referencing the Channel. Any other Player objects in the allPeople collection on the Channel will have their myChannel reference set to null (because the Channel to which they're related is being deleted).

Not knowing anything about your system of course, it doesn't make sense to me for Player to be the parent. If you want anything to be the parent, should it not be the Channel? Typically in a one-many parent-child relationship, the class implementing the many end is the parent (think of Company and Department, or Department and Employee). If you delete a Player, you probably don't want the Channel to which the Player is related to be deleted as well, because there may be other Players involved with the Channel. Is it valid for a Player to exist without being related to a Channel? If so, the relationship should probably be peer-to-peer. If not, Channel should probably be the parent.

Dean.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Parent and Child things again.

Postby ConvertFromOldNGs » Fri Aug 07, 2009 11:50 am

by Darrell Duniam >> Wed, 12 Apr 2000 11:53:02 GMT

Further to making the relationship peer-to-peer, why have the destructor of each Player do a causeEvent on Channel when they disappear - when the Channel object responds to the Event, then if allPeople.isEmpty is true, then Channel could delete itself.

darrell.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Parent and Child things again.

Postby ConvertFromOldNGs » Fri Aug 07, 2009 11:50 am

by Craig Shearer >> Thu, 13 Apr 2000 1:12:20 GMT

Surely this is simply a reference counting exercise... ie, when all the Players have gone away, the Channel gets deleted. JADE doesn't directly support such a mechanism, but it's pretty easy to build one, as Darrell outlined. However, I think that the causeEvent approach is wrong - you really want this to occur syncnronously - ie. if your objects are persistent, then you'd want them to be deleted in the same transaction, and the causeEvent doesn't allow this.

A better approach would be to have the destructor of the Player send a message to its Channel that it was being deleted, and when the Channel saw there were no more Players, it would then delete itself. Something like:

Player::delete() updating;

vars
channel : Channel;begin

channel := myChannel;
myChannel := null; // set myChannel to null to cause the Channel's collection to be updated
channel.notifyDisconnectPlayer();
end;

Channel::notifyDisconnectPlayer() updating;
begin

if allPeople.isEmpty then
delete self;
endif;
end;



Comments?

Craig.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Parent and Child things again.

Postby ConvertFromOldNGs » Fri Aug 07, 2009 11:50 am

by Darrell Duniam >> Thu, 13 Apr 2000 1:26:05 GMT

Yep, I agree. This would also reduce the likelyhood of a new Player joining the Channel between the time when the "last" player left and the Channel deleted itself (ie. between the time it takes for the event notification to be sent and handled by the Channel). Although joining Players should protect themselves against the Channel object not being there anyway.

darrell.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Parent and Child things again.

Postby ConvertFromOldNGs » Fri Aug 07, 2009 11:51 am

by Allistar Melville >> Thu, 13 Apr 2000 10:03:49 GMT

The way I would do it is to set them up peer - peer but make it so the only way of deleting the player be controlled via the channel.

i.e:

Channel::removePlayer(player: Player);

vars
begin

delete player;
if (allPlayers.isEmpty()) then
delete self;
endif;
endif;

This way there is only one method to write with and it means the
channel is responsible for its players. (Similarly I would put an ::addPlayer method on the Channel as well).

Just my thoughts.

Allistar.

------------------------------------------------------------------
Allistar Melville
Software Developer
Auckland, NEW ZEALAND

Greentree International,
Developer of Greentree Financial Software. ------------------------------------------------------------------

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Parent and Child things again.

Postby ConvertFromOldNGs » Fri Aug 07, 2009 11:51 am

by JADE Kid - Ray Hidayat >> Fri, 14 Apr 2000 23:51:56 GMT

That would have been a better idea. But what I do is I set up a mapping method of myChannel, so that when the person leaves, it has to check if anyone is in there. Otherwise delete it. It works, but it isn't the best way.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Parent and Child things again.

Postby ConvertFromOldNGs » Fri Aug 07, 2009 11:51 am

by JADE Kid - Ray Hidayat >> Thu, 13 Apr 2000 4:09:04 GMT

Well, first thing: Why is Player the parent and not channel?

Well I want the player to keep existing, and keep all his scores and things. But the channel is only a chatroom and you can have lots of those, so that the players can have private chatting sessions and clans and all that. But when there aren't any players in a channel, I want it deleted. I could have made this transient, right? I have actually already done that, but I was looking for an easier way.

If it was the channel that was the parent, it would never get deleted, even when no one is chatting and it is not in use. That would be a bit stupid.

Second thing: The notification system:

I feel that this is the first system which I have taken advantage with the object oriented things. Usually I will just use classes to put data in, just like a boring old access table. But objects (AJ told me at the training course) have 3 things. Identity, Behavior and State. Now I give every class each of those things, and because the methods are lying everywhere, I am using heaps of notifications.

I don't need to include a player leave notification, because it is set to a mapping method of the player's my channel to know if it should delete the channel or not. That is pretty simple. The chat window has about 7 notifications on it. When you are casting a kick vote it will have two more, and you can kick more than one person at a time in a channel.

Is 7 notifications a lot, or is it a reasonable size? Does anyone have huge amounts of notifications?


Third section: A few questions...

Gosh, I never knew that there was a method called .isEmpty! That would be useful because I am currently using .size = 0!
Do you think that to have a parent collection option to delete the child when all parents disappear is a good idea? Because I am using the same principle in my Game class. Just substitute channel for 'Game'.
I can't actually see many examples of this happening; this is very unusual.

--
Ray Hidayat
JADE Kid - 2000
www.jadekids.com


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 4 guests

cron