Java Reference
In-Depth Information
interface should define any of the methods that would be used to build the object as
well as a method that will return a fully built object. In this case, the interface will
define each of the methods needed to build a new Team object, and then the builder
implementation will implement this interface.
public interface TeamBuilder {
public void buildPlayerList();
public void buildNewTeam(String teamName);
public void designateTeamCity(String city);
public Team getTeam();
}
The following code demonstrates a builder class implementation. Although the fol-
lowing code would not create a custom player list, it contains all the features required
to implement the builder pattern. The details of creating a more customized player list
can be worked out later, probably by allowing the user to create players via a keyboard
entry. Furthermore, the TeamBuilder interface could be used to implement teams
for different sports. The following class is named HockeyTeamBuilder , but a sim-
ilar class implementing TeamBuilder could be named FootballTeamBuilder ,
and so forth.
public class HockeyTeamBuilder implements TeamBuilder {
private Team team;
public TeamBuilder(){
this.team = new Team();
}
@Override
public void buildPlayerList() {
List players = new ArrayList();
for(int x = 0; x <= 10; x++){
players.add(PlayerFactory.getPlayer());
}
team.setPlayers(players);
}
Search WWH ::




Custom Search