A construction pattern for dependent objects

satya - 10/19/2013 10:23:17 AM

Problem

Sometimes in a graph the dependent objects need to be registered with a parent once they are constructed.

This relationship may not be obvious to the designer. Resulting in doing things like the following


Parent
{
    create a child
    register the child
}

satya - 10/19/2013 10:24:43 AM

The obvious solution is


Parent
{
   create a child
}

Child
{
   constructor(parent)
   {
      //do your own construction

      //then register the child
      parent.register(this);
   }
}

satya - 10/19/2013 10:25:45 AM

This relationship is not obvious because


child 
{
  //No explicit pointer required for the parent
  //So it is easy to forget not to take the parent 
  //as a constructor
}