My question in the context of Method Chain + Inheritance doesn't fit well? . But, unfortunately, all the examples / answers of the method chain use the same level of inheritance. My account includes multi-level inheritance like
abstract class PetBuilder{...} class DogBuilder extends PetBuilder{..} class DogType1Builder extends DogBuilder {...}
To create a Dog object, I will use either DogBuilder or DogType1Builder
how to use getThis trick for the above use case?
I want to use a template to build a complex Dog object (Dog Object Model) . DogType1 will have some additional properties.
so to use the getThis Trick declaration above the classes would look like
abstract class PetBuilder<T extends PetBuilder<T>> class DogBuilder<T extends DogBuilder<T>> extends PetBuilder<DogBuilder<T>> class DogType1Builder extends DogBuilder<DogType1Builder>
Now this creates two problems
Method 1.builder in "DogBuilder" will look like
public T someMethodInDog(String dogName) { .. return (T)this;
2.As DogBuilder has become parameterized, so to create an instance of "DogBuilder" I have to use
DogBuilder<DogBuilder> builder=new DogBuilder();
Is there a better way?
source share