My team has been using PojoBuilder at Rubicon Project for the last 6 month or so. We use it in conjunction with Google's AutoValue project to simplify the tedium of writing correct immutable data models. It addressed the problem we're trying to solve. However, it did feel clunky. Typically, a data model looks something like this:
@AutoValue
public abstract class Employee {
@BasePojoBuilder
public static Employee create(String id, String firstName, String surname...) {
return new AutoValue_Employee(id, firstName, surname...);
}
public abstract String getId();
public abstract String getFirstName();
public abstract String getSurname();
...
}
Today I discovered that the AutoValue project will be adding support for the builder pattern:
import com.google.auto.value.AutoValue;
class Example {
@AutoValue
abstract static class Animal {
static Builder builder() {
return new AutoValue_Example_Animal.Builder();
}
abstract String name();
abstract int numberOfLegs();
@AutoValue.Builder
interface Builder {
Builder name(String s);
Builder numberOfLegs(int n);
Animal build();
}
}
}
I'm eagerly waiting for this to come out of *1.1-SNAPSHOT* into GA release. It'll further simplify tedious parts of coding!
No comments:
Post a Comment