mkarneim / pojobuilder

A Java Code Generator for Pojo Builders
Other
334 stars 44 forks source link

Pojo with interface #102

Closed uvula closed 7 years ago

uvula commented 9 years ago

I just starting to examine your pojobuilder. A question: if the pojo implements an interfaces. How can the builder return the Interface? class Person implemnts IPerson ... IPerson p = new PersonBuilder()....build();

Must I use the Factory approach? I tried this, it works.

mkarneim commented 9 years ago

Yes, that's right.

Since an interface has no constructor implementation, you have to provide a factory method to create instances of it. Just annotate that factory method, and PB knows how to build instances.

Here is an example:

public class IPersonFactory {

  @GeneratePojoBuilder
  public static IPerson create(String name) {
    Person result = new Person(name);
    return result;
  }
}