vendredi 17 juillet 2015

POJO multiple-hierarchy Builder in Java

Im trying to achieve a hierarchy of builder with more than 3 levels.

Something like

  public abstract class ElementBuilder  {
    protected String name;

    public ElementBuilder setName(String name) {
        this.name = name;
        return this;
    }
  }

  public abstract class OperationBuilder extends ElementBuilder {
     protected String attribute;

     public OperationBuilder setName(String attribute) {
         this.attribute = attribute;
         return this;
     }
  }



 public abstract class FilterBuilder extends OperationBuilder {
     ....
 }

The problem is that when I call an operation of the super class its returning a builder of the that class, I dont want to duplicate the setter method in each child, cause it maybe contain some logic.

I tried using generics but I could not achieve it in a clean way.

Thanks in advance, Juan

Aucun commentaire:

Enregistrer un commentaire