Java Generics question, daisy-chaining object calls

I would have to add that I would not recommend this patten. If the builder is so complex it needs to be composed out of different objects, that is probably a sign that the resulting object has far too many responsibilities, and should be split itself.

That’s definitely necessary method.

Turns out I was wrong; this was quite doable when abstracting the self reference:

interface Builder<T> {
  T self();
}

abstract class Alpha<T> implements Builder<T> {
  public T doAlpha() {
    System.out.println("alpha");
    return self();
  }
}

abstract class Beta<T> extends Alpha<T> {
  public T doBeta() {
    System.out.println("beta");
    return self();
  }
}

abstract class Gamma<T> extends Beta<T> {
  public T doGamma() {
    System.out.println("gamma");
    return self();
  }
}

class Greek extends Gamma<Greek> {
  @Override
  public Greek self() {
    return this;
  }
}

public class Main {

  public static void main(String[] args) {
    new Greek()
              .doAlpha()
              .doAlpha()
              .doGamma()
              .doBeta()
              .doGamma()
              ;

  }
}

It has to be a class hierarchy, not interfaces. While some of the classes can be abstract, a few of them will not be. We also do not want the finial class to simply override every method presented in all base classes. That is just the god interface solution done backwards. Also having 10 interfaces that define various methods that the final classes have to implement is no different than a giant god interface.

I think it will be highly likely that you will regret it later if your teammate manages to do this.

It sounds like he is creating a highly complex structure using patterns that are widely thought to be an anti-pattern in a language that is not designed to do what he is trying to do.

I’ve been on projects where somebody did stuff like that. It never ends well. I hope you guys escape unscathed :D

What are the mitigating circumstances that are forcing this? Is there legacy code involved? (Or is it the Boss?)

It is just something he wanted to do. I do not know why. If he asks again about this subject, I think Ill question him further on it. I really think its a bad idea. I have a feeling it is “Just because”.

Yeah I have no idea what he could be doing where something like this would seem like a good idea. If the answer is “Just because” then… the whole situation make me think of this:

Summary

https://i.imgur.com/xioMcFe.jpg

I will not be suprised that the real reason may be that It().is().so().that().i().can().write().code().like().this();