Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OO Design Question -- Parent/Child(ren) -- Circular?

I'm fairly new to the OO design process, so please bear with me....

I have two entities that I need to model as classes, call them Parent and Child (it's close enough to the actual problem domain). One Parent will have one or more Children -- I have not interest, in this application, in childless Parents.

Where my brain is going out to lunch is on the fact that I need to be able to find either from the other. In my database I can implement this with a normal foreign key relationship, and the set-based nature of SQL makes it easy to find all Children for a given Parent, or the Parent for a given Child. But as objects...?

I think that the Parent should carry a collection (list, whatever) of Children. I also think that each Child should carry a reference to its Parent. The circular nature of the references, however, is making my head hurt.

Am I:

  • On the right track?
  • Completely off base? If so, what should I do differently?

This will almost certainly be implemented in VB.NET, but I'm a ways from cutting code yet.

Edit after 8 answers:

Thanks all. It was hard to pick just one answer to accept.

To clarify a couple of things that were questioned in the answers:

  • Parent and Child are very different entities--there's not inheritance relationship at all. I chose the names that I did because they're really very close to the real-world problem domain, and now see that it's a source of confusion from an OO perspective.
  • The hierarchy is only one level deep--Children will never have Children within the application.

Thanks again.

like image 299
RolandTumble Avatar asked Jun 22 '09 19:06

RolandTumble


5 Answers

The circular references are fine and absolutely standard when creating a tree structure. HTML's Document Object Model (DOM), for example, has the parent and child properties on every node in a DOM tree:

interface Node {
    // ...
    readonly attribute Node     parentNode;
    readonly attribute NodeList childNodes;
    // ...
}
like image 54
John Kugelman Avatar answered Nov 07 '22 06:11

John Kugelman


Sounds like you're on the right track to me. As per your domain model, parents have children and children have parents. You may need to reference each from the other.

There is nothing wrong with circular references, you just have to be careful about what you do with them. Where you'll run into trouble is managing your entities on the server side in an automated fashion when you load them from the database. For example, you fetch a Child object from the database with a query. Do you include the parent information? Do you include the parent's children?

ORM tools like Lightspeed or Microsoft's Entity Framework generally deal with this using "lazy loading" directives. They'll fetch what you need at first (so, when you fetch a Child, it just gets the Child properties and the parent's ID). If later, you dereference the Parent, it goes and fetches the Parent properties and instantiates the Parent object. If later still, you access it's Children collection, it then goes and fetches the relevant child information and creates Child objects for that collection. Until you need them though, it doesn't populate it.

like image 45
womp Avatar answered Nov 07 '22 06:11

womp


I think it's reasonable to want to be able to traverse the object graph in this way. It's hard to know if you have a justifiable reason for it from your post, but I don't think the references in and of themselves prove a bad design.

like image 29
Robert Avatar answered Nov 07 '22 06:11

Robert


I believe you're on the right track. Why is the circular nature of the references making your head hurt? What is the fundamental issue you're having with a Parent having references to its children, and a Child having a reference to its parent?

like image 2
Paul Sonier Avatar answered Nov 07 '22 04:11

Paul Sonier


Are you talking about a class hierarchy, where the parent class knows about its child classes?

You should avoid this at all costs.

By default, a child class knows all about a parent class, because it is an instance of the parent class. But to have a parent class know about its child classes requires that the child class also know all about every other child class. This creates a dependency between one child and every other child of that class. This is an unmaintainable scenario that will cause problems in the future -- if you can even get it to compile or run, which in many languages will not be the case.

That said, it sounds to me like you're not trying to do a class hierarchy, but a collection hierarchy, i.e. a tree. In that case, yes, you're on the right track; it's a common paradigm. The parent node has a collection of child nodes, and the child node has a reference to the parent node.

The thing is? They're all the same class! Here's a very simple example in C#:

public class Node
{
  public readonly Node Parent; // null Parent indicates root node
  public readonly List<Node> Children = new List<Node>();
  public Node(Node parent)
  {
     Parent = parent;
  }
  public Node()
  {
     parent = null;
  }
  public void AddChild(Node node)
  {
     Children.Add(node);
  }
}

I have a feeling this is what you're really after. Using this paradigm, you would then sub-class Node for whatever nefarious purposes you might have.

like image 2
Randolpho Avatar answered Nov 07 '22 04:11

Randolpho