title: Java Model Objects design
body: <p>So I've been reading some <em>Effective Java</em>! And one of the most inspiring sections of the book is the Immutable Object/Builder section where Bloch writes about the "Builder" - class instead of just POJOs.</p>
  
  <p>NOTE: I am talking about model objects here: like for instance an <em>Article</em> or a <em>Car</em>.</p>
  
  <p>This is how I wrote these objects before:</p>
  
  <pre><code>public class Car {
  
       private String name;
  
       public void setName(String name) {
           this.name = name;
       }
  
       public String getName() {
           return name;
       }
  }
  </code></pre>
  
  <p>Now as you see this design is deficient in a number of ways, it requires mutability and you have to construct the object with first the constructor then setting the name. </p>
  
  <p>Now of course you can make the <code>name</code> field final and use it as an argument in the constructor but then if you have a large object wrapping for instance a number of SQL - Tables then you will have an ugly constructor like this:</p>
  
  <pre><code>public Car(int horsepowers, String name, String brand, int yearManufactured,
      User owner, List&lt;User&gt; previousOwners) {
      //Set the values
  }
  </code></pre>
  
  <p>This becomes unreadable when creating the object, and this is just six fields!</p>
  
  <p>So, Bloch suggests the following (with immutability)</p>
  
  <pre><code>public class Car {
  
       public static class Builder {
  
           private String name;
  
           public Builder setName(String name) {
               this.name = name;
               return this;
           }
  
           public Car build() {
               reeturn new Car(this);
           }
       }
  
       private final String name;
  
       private Car(Builder builder) {
           name = builder.name;
       }
  
       public String getName() {
           return name;
       }
  }
  
  //Construction example
  Car car = new Car.Builder().setName("Speedy").build();
  </code></pre>
  
  <p>Now this gives us immutability! And if you have some objects that arent primitive or immutable just copy them in the <code>Builder</code>'s setters and copy them again in the <code>Car</code>'s getters.</p>
  
  <p>But it's very wordy and I've been using constructor arguments if the class is small enough.
  If a class needs a mutable field I just make that field mutable, if the class has enough properties (> 4 something).</p>
  
  <p>Another problem is when working with android and the class has for example a <code>Bitmap</code>, then you have to return the actual bitmap and not copy it because that is rather performance - expensive. </p>
  
  <p>I've seen tons of questions like this but I can't seem to find a good answer on this question: is there any <em>standard</em> on these designs and how are their design? What are the benefits/defeceits? </p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>The question is:</p>
  
  <p>What is the <em>best</em> way to construct an object model that should be immutable and with A) a small number of fields and B) a large number of fields? How to handle the <code>Bitmap</code> issue mentioned above and similar issues? Making certain fields mutable? </p>
  
  <p>Sorry for being vague.</p>