Java 17 Recipes


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

 How It Works
The builder pattern provides a way to generate new instances of an object in a 
procedural fashion. It abstracts away the details of object creation, so the creator does 
not need to do any specific work to generate new instances. By breaking the work down 
into a series of steps, the builder pattern allows objects to implement its builder methods 
in different ways. Because the object creator only has access to the builder methods, it 
makes creating different object types much easier.
A few classes and interfaces are necessary for using the builder pattern. First, 
you need to define a class and its different attributes. As the solution to this recipe 
demonstrates, the class may follow the JavaBean pattern. You can populate the object by 
using its setters and getters by creating a JavaBean. Next, you should create an interface 
that can be used to access the setters of the object you created. Each setter method 
should be defined in the interface, and then the object itself should implement that 
interface. As seen in the solution, the Team object contains the following setters, and each 
of them is defined in the TeamType interface.
public void setPlayers(List
players);
public void setName(String name);
public void setCity(String city);
In real life, a team probably contains more attributes. For instance, you’d probably 
want to set up a mascot and a home stadium name and address. The code in this 
example can be thought of as abbreviated because it demonstrates the creation of a 
generic “team object” rather than showing you all the code for creating a team that is 
true to life. Because the Team class implements these setters that are defined within the 
TeamType interface, the interface methods can be called on to interact with the actual 
methods of the Team class.
After the object and its interface have been coded, the actual builder needs to be 
created. The builder consists of an interface and its implementation class. To start, you 
must define the methods you want other classes to call on when building your object. 
Chapter 5 ObjeCt-Oriented java


178
For instance, in the solution to this recipe, the buildNewTeam(), designateTeamCity(), 
and buildPlayerList() methods are defined within the builder interface named 
TeamBuilder. When a class wants to build one of these objects later, it only needs 
to call these defined methods. Next, define a builder class implementation. The 
implementation class implements the methods defined within the builder interface, 
hiding all the details of those implementations from the object creator. The builder class, 
HockeyTeamBuilder, implements the TeamBuilder interface in the solution to this recipe. 
When a class wants to create a new Team object, it simply instantiates a new builder class.
TeamBuilder builder = new HockeyTeamBuilder();
To populate the newly created class object, the builder methods are called upon it.
builder.buildNewTeam(teamName);
builder.designateTeamCity(city);
builder.buildPlayerList();
Using this technique provides a step-by-step creation for an object. The 
implementation details for building that object are hidden from the object creator. It 
would be easy for a different builder implementation to use the same TeamBuilder 
interface to build Team objects for different types. For instance, a builder implementation 
could be written for generating Team objects for soccer, and another one could be defined 
for generating Team objects for baseball. Each of the Team object implementations would 
be different. However, both could implement the same interface—TeamBuilder—and 
the creator could simply call on the builder methods without caring about the details.

Download 3.2 Mb.

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




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