Created
August 31, 2016 08:38
-
-
Save r-winkler/d358036d57df9d32e11cc922ebcd7e9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Instrument { | |
private String name; | |
private Instrument(Builder builder) { | |
this.name = builder.name; | |
} | |
public static class Builder { | |
private String name; | |
private ListBuilder listBuilder; // parent builder | |
public Builder withName(String name) { | |
this.name = name; | |
return this; | |
} | |
public Instrument build() { | |
return new Instrument(this); | |
} | |
// setter for parent builder | |
public Builder setListBuilder(ListBuilder builder) { | |
this.listBuilder = builder; | |
return this; | |
} | |
// build it and get parent builder again | |
public ListBuilder toList(){ | |
this.listBuilder.add(this.build()); | |
return this.listBuilder; | |
} | |
} | |
public static class ListBuilder { | |
private List<Instrument> instruments = new ArrayList<>(); | |
private Airplane.Builder airplaneBuilder; // parent builder | |
public ListBuilder add(Instrument instrument){ | |
this.instruments.add(instrument); | |
return this; | |
} | |
public List<Instrument> build(){ | |
return this.instruments; | |
} | |
// get child builder and hands over parent builder | |
public Instrument.Builder add() { | |
return new Instrument.Builder().setListBuilder(this); | |
} | |
// setter for parent builder | |
public ListBuilder setAirplaneBuilder(Airplane.Builder builder){ | |
this.airplaneBuilder = builder; | |
return this; | |
} | |
// build it and get parent builder again | |
public Airplane.Builder done() { | |
this.airplaneBuilder.addList(this.build()); | |
return airplaneBuilder; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment