Java Reference
In-Depth Information
rosters for baseball teams tend to be fairly small), but we know that there has to be a better
way.
As always, we should start by deciding on the interface that we want to support for this kind
of access. Since we want to find particular Player objects that are associated with a particu-
lar Team , we need to add something to the Team interface. We want a method that will return
a Player object associated with a Team , given some identification of that Player . Since all
Player objects have a unique identifier, finding Player objects by that identifier seems like
an obvious addition to the interface. But since the unique identifiers are not very easy for hu-
mans to remember, it would also be nice to have a method that would return a Player object
given something easier for people to understand, such as the name of the player. This would
suggest adding two methods along the lines of:
Player getPlayer(UUID playerID);
Player getPlayer(String playerName);
The first of these is fine, but the second needs to be refined a bit. Recall that we introduced
the player identifier because of the possibility that two players could have the same name. But
if that is possible, it is also possible that two players on the same team could have the same
name. So the second of these methods can't be guaranteed to return a single player given a
name. Instead, we will allow the method that finds Player objects by name to return all of the
Player objects associated with the name in a Set . Usually, the Set will have a single Player
object in it (or no Player objects if there is no one by that name on the team), but if there are
duplicate names on the same team, we can return both Player objects and let the caller figure
out which one she really wanted.
With this in mind, we change our Team interface to allow finding Player objects by identifier
or by name so that it looks like:
package org.oreilly.javaGoodParts.examples.statistics;
import java.util.Set;
import java.util.UUID;
/**
* The {@code Team} interface, which defines the notion of a team for our
* statistics package. A team is, at first incarnation, simply a collection of
* players. All teams have a name.
*/
Search WWH ::




Custom Search