Item 2. Class Builders

Consider a builder when faced with may constructor parameters.

Used when static factories and constructors share a large number of optional parameters.  Used in place of telescoping constructor patterns.  May involve the use of a builder pattern.  Invariants can be imposed on the builder parameters.  If any invariants are violated, an IllegalStateException must be thrown, with the exception detail message indicating which invariant is violated.  This should be used if there are more than four constructor parameters.

A good choice when designing classes whose constructors or static factories would have more than a handful of parameters.

Advantages

  1. makes client code easier to write and read when there are may parameters.
  2. reduces mental counting of parameters supplied to constructors.
  3. improves accuracy of parameters supplied to constructors.
  4. improves on JavaBeans constructor pattern because it prevents inconsistent state partway through construction.
  5. Improves on JavaBeans constructor pattern because it allows a class to be made immutable.
  6. requires less effort than JavaBeans constructor pattern to ensure thread safety.
  7. Can have multiple varargs parameters.
  8. A single builder can be used to build multiple objects. Parameters of the builder can be tweaked between object creations to vary the objects.
  9. some fields can be filled in automatically each time the object is created.
  10. can be used as an Abstract Factory when the parameters have been set.
  11. Avoids breaking compile-time exception checking that happens with Class.newInstance.

Disadvantages

  1. In order to create an object you must first create its builder.  Could be problematic in some performance-critical situations.

My Own Additional Notes

The need to create object builders every time they are used could be reduced by using a static object builder.  That is a builder that is created once, but is “cleared” prior to every use.

public final class Foo {
     /**
      * The only place fooBuiler is only used is in 
      * {@link #newInstance} - to create Foo objects.
      */
     private static Builder fooBuilder = new Builder();

     public static Foo newInstance(){
          // reset to a clean state.
          fooBuilder.clear();
          fooBuilder.setParamater1("one");
          fooBuilder.setParameter2("two");
          return fooBuilder.build();
     }
}
Advertisements

One thought on “Item 2. Class Builders

  1. Pingback: Effective Java #2: Class Builders | Richard's Mobile Blog

And now it's your turn ... comment here or on Twitter (@Androider)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s