lundi 1 juin 2020

Builder patterns and genertics type that extends itself

I am trying to implement Builder pattern with inheritance and found the following solution .

Now I want to make class C also extendable. This is the way I do it:

public class C extends B {
    public static class Builder<T extends Builder<T>> extends B.Builder<T> {

    }
}

So, in C we have builder that can be extended in, for example, D. My question is what is the right way to create C.Builder instance. I mean, what I should set in its generics. I mean something like:

C.Builder<WHAT HERE> builder = new C.Builder<>();

Aucun commentaire:

Enregistrer un commentaire