Builder pattern for polymorphic object hierarchy: possible with Java?

后端 未结 6 849
谎友^
谎友^ 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:48

    Use generics, as follows:

    public interface Parent {
        public Long getParentProperty();
    }
    
    public interface Child {
        public T getChildProperty(); 
    }
    

    Then instead of Child1, use Child and instead of Child2, use Child.

提交回复
热议问题