title: How to Develop a Composite / Builder Design Pattern
body: <p>I'm working on a java project that requires the build of composite objects. In short, I'm using a variation of the <a href="http://patterns.instantinterfaces.nl/current/Refactoring-and-Design-Patterns-PAT-BLD.html" rel="nofollow">Builder Pattern</a> to build out an "experiment." I need to also create a builder to create the users from the experiment users field in the experiment model.</p>
  
  <blockquote>
    <p>The rub is that I'm not sure how to implement designing a <a href="http://patterns.instantinterfaces.nl/current/Refactoring-and-Design-Patterns-PAT-PARS.html#PAT-PARS-COP" rel="nofollow">Composite Design Pattern</a> object to build out the <code>experiment.users</code> when I build out the experiment.</p>
  </blockquote>
  
  <p>Not germane to the topic but I'm using SpringMVC on the front end and using JPA for the persistence layer... </p>
  
  <p><code>The Service</code></p>
  
  <pre><code>public interface ExperimentService {
  
      public Experiment create( ExperimentDTO created );
  
      ... 
  
  }
  </code></pre>
  
  <p><code>The Controller</code></p>
  
  <pre><code>    @RequestMapping(value = "/experiment/create", method = RequestMethod.POST)
      public String submitCreateExperimentForm(@Valid @ModelAttribute(MODEL_ATTRIBUTE_EXPERIMENT) ExperimentDTO created, BindingResult bindingResult, RedirectAttributes attributes) {
  
      ...
      Experiment experiment = experimentService.create(created);
  
      ... 
  }
  </code></pre>
  
  <p>Attempting to follow good design habits, I'm try to avoid building an <a href="http://www.javacodegeeks.com/2011/09/on-domain-driven-design-anemic-domain.html" rel="nofollow">Anemic Domain Model</a> by following the classic builder pattern within the experiment entity. However, I'm a bit stuck in figuring out the best workflow to call my UserBuilder Builder from the ExperimentBuilder... </p>
  
  <p><code>The Builder</code></p>
  
  <pre><code>public interface Builder&lt;T&gt; {
  public T build();
  }
  </code></pre>
  
  <p><code>Experiment Entity</code> </p>
  
  <pre><code>@Entity(name = "experiment")
  public class Experiment implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Long id;
  
     @OneToMany
     @JoinTable(
          name = "experiment_participant",
          joinColumns = @JoinColumn( name = "experiment_id"),
          inverseJoinColumns = @JoinColumn( name = "participant_id")
     )
     private Set&lt;ExperimentUser&gt; experimentUsers = new HashSet&lt;&gt;();
  
      /** fields ... getters ... setters .. **/
  
     public static class ExperimentBuilder implements Builder {
  
      private final Experiment experiment;
     /** fields ... **/
  
      public ExperimentBuilder( ... ) {
          experiment = new Experiment();
  
       /** ??? WIRE UP THE USERBUILDER HERE ??? ** **/ 
  
      }
  </code></pre>