Java 17 Recipes


-8. Constructing Instances of the Same Class with


Download 3.2 Mb.
Pdf ko'rish
bet101/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   97   98   99   100   101   102   103   104   ...   245
Bog'liq
Java 17 Recipes

5-8. Constructing Instances of the Same Class with 
Different Values
 Problem
Your application requires the ability to construct instances of the same object, but 
each instance must contain different values, thereby creating different types of the 
same object.
 Solution
Use the builder pattern to build different types of the same object following a step-by- 
step procedure. For instance, suppose that you are interested in creating different teams 
for a sports league. Each team must contain the same attributes, but the values for those 
attributes vary by team. So you create many objects of the same type, but each of the 
objects is unique. The following code demonstrates the builder pattern, which creates 
the required teams.
First, you need to define a set of attributes that each team needs to contain. To do 
this, a Java interface should be created, containing the different attributes that need to be 
applied to each Team object. The following is an example of such an interface.
public interface TeamType {
public void setPlayers(List
players);
public void setName(String name);
public void setCity(String city);
public String getFullName();
}
Next, define a class to represent a team. This class needs to implement the TeamType 
interface that was just created so that it adheres to the format that is required to 
build a team.
public class Team implements TeamType {
private List
players;
private String name;
private String city ;
private int wins
Chapter 5 ObjeCt-Oriented java


174
private int losses
private int ties
/**
* @return the players
*/
public List
getPlayers() {
return players;
}
/**
* @param players the players to set
*/
@Override
public void setPlayers(List
players) {
this.players = players;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
@Override
public void setName(String name) {
this.name = name;
}
/**
* @return the city
*/
public String getCity() {
return city;
}
/**
Chapter 5 ObjeCt-Oriented java


175
* @param city the city to set
*/
@Override
public void setCity(String city) {
this.city = city;
}
public String getFullName(){
return this.name + " – " + this.city;
}
}
Now that the Team class has been defined, a builder needs to be created. The purpose 
of the builder object is to allow the step-by-step creation of a Team object. A builder class 
interface should be created to abstract the details of building an object. The interface 
should define any of the methods that build the object and a method that return a fully 
built object. In this case, the interface defines each of the methods needed to build a new 
Team object, and then the builder implementation implements 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 following 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 implement teams for 
different sports. The following class is named HockeyTeamBuilder, but a similar class 
implementing TeamBuilder could be named FootballTeamBuilder, and so forth.
public class HockeyTeamBuilder implements TeamBuilder {
private Team team; 
public HockeyTeamBuilder(){
Chapter 5 ObjeCt-Oriented java


176
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);
}
@Override
public void buildNewTeam(String teamName) {
team.setName(teamName);
}
@Override
public void designateTeamCity(String city){
team.setCity(city);
}
@Override
public Team getTeam(){
return this.team;
}
}
Last, use the builder by calling on the methods defined in its interface to create 
teams. The following code demonstrates how this builder could 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();
}
Chapter 5 ObjeCt-Oriented java


177
Although this demonstration of the builder pattern is relatively short, it demonstrates 
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 do; you only need to call 
on them.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   97   98   99   100   101   102   103   104   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling