Java Reference
In-Depth Information
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 implement-
ation details for building that object are hidden from the object creator. It would be
easy enough for a different builder implementation to use the same TeamBuilder in-
terface for building team objects for different types. For instance, a builder implement-
ation 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 implementa-
tions would be different. However, both of them could implement the same inter-
face— TeamBuilder —and the creator could simply call on the builder methods
without caring about the details.
5-9. Interacting with a Class via Inter-
faces
Problem
You have created a class that implements an interface or class type. You would like to
interact with the methods of that class by working with the interface rather than work-
ing directly with the class.
Solution
Declare a variable of the same type as an interface. You can then assign classes that im-
plement the interface to that variable and call upon the methods declared in the inter-
face to perform work. In the following example, a variable is declared to be of type
TeamType . Using the same classes from Recipe 5-8, you can see that the class Team
implements the TeamType interface. The variable that is created in the following ex-
ample holds a reference to a new Team object.
Search WWH ::




Custom Search