title: Implementation pattern of object composition
body: <p>I'm having trouble mapping these classes:</p>
  
  <ul>
  <li><strong>Order</strong></li>
  <li><strong>Pallet</strong></li>
  <li><strong>Box</strong></li>
  </ul>
  
  <p>Together they form a relationship of composition, detailed in <a href="http://en.wikipedia.org/wiki/Object_composition" rel="nofollow">this wikipedia article</a>.</p>
  
  <p>An instance of class <strong>Order</strong> has several <strong>Pallet</strong> objects, which has several <strong>Box</strong> objects.</p>
  
  <p>This is what I have now:</p>
  
  <pre><code>class Order
  {
      public Order(){}
      private List&lt;Pallet&gt; _pallets = new List&lt;Pallet&gt;;
      public Pallet Add_pallet()
      {
          Pallet new_pallet = new Pallet(this);
          this._pallets.Add(new_pallet);
          return new_pallet;
      }
      //...other properties and methods...
  }
  </code></pre>
  
  <hr>
  
  <pre><code>class Pallet
  {
      private Order _parent_order;
      private List&lt;Box&gt; _boxes = new List&lt;Box&gt;;
      public Pallet(Order parent_order)
      {
          _parent_order = parent_order;
      }
      public Box Add_Box()
      {
          Box new_box = new Box(this);
          this._boxes.Add(new_box);
          return new_box;
      }
      //...other properties and methods...
  }
  </code></pre>
  
  <hr>
  
  <pre><code>class Box
  {
      private Pallet _parent_pallet;
      public Box(Pallet parent_pallet){}
      //...other properties and methods...
  }
  </code></pre>
  
  <p>All <strong>Boxes</strong> must belong to a <strong>Pallet</strong> and all <strong>Pallets</strong> must belong to an <strong>Order</strong>.<br>
  How can I implement this correctly to avoid this:</p>
  
  <pre><code>static class Program
  {
      static void Main()
      {
          Order myOrder = new Order();
          Pallet myPallet = new Pallet(myOrder);
          //Now I have a Pallet that belongs to an Order,
          //but the Order object is not aware of it
      }
  }
  </code></pre>
  
  <p>Should I use nested classes or is there something more useful I am missing?</p>