Builder pattern for polymorphic object hierarchy: possible with Java?

后端 未结 6 842
谎友^
谎友^ 2020-12-08 01:11

I have a hierarchy of interfaces, with Child implementing Parent. I would like to work with immutable objects, so I would like to design Bui

6条回答
  •  太阳男子
    2020-12-08 01:55

    The solution I imagine is like the Curiously Recurring Template Pattern, or CRTP. You can define a base class to handle the parent-related initialization, but you still may find the two boilerplate getParent() and getThis() methods to be too much repetition in each derived child-related builder class.

    Take a look:

    abstract class ParentBase implements Parent
    {
      @Override
      public final Long getParentProperty()
      {
          return parentProperty_;
      }
    
    
      protected void setParentProperty(Long value)
      {
          parentProperty_ = value;
      }
    
    
      private Long parentProperty_;
    }
    
    
    abstract class ParentBuilder>
    {
      T withParentProperty(Long value)
      {
          getParent().setParentProperty(value);
          return getThis();
      }
    
    
      protected abstract ParentBase getParent();
    
    
      protected abstract T getThis();
    }
    
    
    final class ConcreteChild1 extends ParentBase implements Child1
    {
      @Override
      public Integer getChild1Property()
      {
          return childProperty_;
      }
    
    
      public void setChild1Property(Integer value)
      {
          childProperty_ = value;
      }
    
    
      private Integer childProperty_;
    }
    
    
    final class Child1Builder extends ParentBuilder
    {
      public Child1Builder()
      {
         pending_ = new ConcreteChild1();
      }
    
    
      public Child1Builder withChild1Property(Integer value)
      {
          pending_.setChild1Property(value);
          return this;
      }
    
    
      @Override
      protected ParentBase getParent()
      {
          return pending_;
      }
    
    
      @Override
      protected Child1Builder getThis()
      {
          return this;
      }
    
    
      private final ConcreteChild1 pending_;
    }
    

    As you can see, the ParentBuilder type expects to be cooperating with a derived type to allow it to return a properly-typed instance. Its own this reference won't due, because the type of this within ParentBuilder is, of course, ParentBuilder, and not, say, Child1Builder as intended to maintain the "fluent" call chaining.

    I owe the "getThis() trick" to Angelika Langer's tutorial entry.

提交回复
热议问题