Skip to content
mkarneim edited this page Dec 7, 2014 · 14 revisions

A Builder Factory is a method for creating a builder instance with default values.

For example, given the pojo class Order looks like this:

@GeneratePojoBuilder
public class Order {
  public Date date;
  public long id;
  public List<Item> items;
}

If you want to build a new instance of Order with some default values for date and items, add the following factory method to some application utility class:

/**
* This is a builder factory that creates a new OrderBuilder
* with default values.
*/
public static OrderBuilder $Order() {
  return new OrderBuilder()
    .withDate( new Date())
    .withItems( new ArrayList<Item>());
}

The '$'-Sign is just a handy naming convention to distinguish builder related methods and instances.

From your application code you could use the factory method like this:

  Order anOrder = $Order().withId( 100).build();

As result you will get 'anOrder' with id:=100, date:=(today) and items:=(an empty list).