Java Reference
In-Depth Information
@Override
public void buildNewTeam() {
team.setName("The Java 8 Team");
}
@Override
public void designateTeamCity(){
team.setCity("Somewhere in the world");
}
public Team getTeam(){
return this.team;
}
}
Last, use the builder by calling upon the methods defined in its interface to create
teams. The following code demonstrates how this builder could be used to create one
team. You can use the Roster class within the sources for this recipe to test this code:
public Team createTeam(String teamName, String city){
TeamBuilder builder = new HockeyTeamBuilder();
builder.buildNewTeam(teamName);
builder.designateTeamCity(city);
builder.buildPlayerList();
return builder.getTeam();
}
Although this demonstration of the builder pattern is relatively short, it demon-
strates how to hide implementation details of an object, thereby making objects easier
to build. You do not need to know what the methods within the builder actually do; you
only need to call upon them.
How It Works
The builder pattern provides a way to generate new instances of an object in a proced-
ural fashion. It abstracts away the details of object creation, so the creator does not
Search WWH ::




Custom Search