jeudi 29 juin 2017

How to implement a recursive Builder Pattern in Java?

I'm trying to grow a tree via Builder Pattern in Java. This tree has LongBranches, BlueBranches and RedBranches. Each kind of branch can be parent to any other type of branch. In addition, I have a Trunk and Leaf's. As you can see, this is quite complicated.

The tree builder is meant to be used like this:

Sapling.grow()
    .long()
         .red()
              .leaf()
         .end()
    .end()
    .blue()
    .end()
    .red()
         .long()
              .leaf()
              .leaf()
         .end()
    .end()
.end()

I tried to implement this via mutually recursive Builders and generics but only managed to generate (non-working) noodle code.

Please show me a minimal example of how this can be accomplished.

Aucun commentaire:

Enregistrer un commentaire