Java Reference
In-Depth Information
case Catcher:
if (roles.contains(Roles.Fielder)){
catcherStats = new CatcherImpl(fielderStats);
fielderStats = catcherStats;
} else {
catcherStats = new CatcherImpl();
roles.add(Roles.Fielder);
}
break;
}
roles.add(role);
}
public Batter asBatter(){
return batterStats;
}
public Fielder asFielder(){
return fielderStats;
}
public Catcher asCatcher(){
return catcherStats;
}
}
This implementation approach is not the only one we could take (although it does show the
value of the
EnumSet
), and it does have its own problems. The
EnumSet roles
duplicates
information that we already have (in a less direct way) in the
batterStats
,
fielderStats
,
and
catcherStats
fields. Further, we now have two places where we have information that
should be correlated. We could have complicated the implementation of the
hasRole()
meth-
od by determining the answer through examining the various
Stats
fields. Whether you prefer
the more complex implementation or the field correlation is, to some extent, a matter of taste
and style. Either can be right, and both can have their problems.
Now, we did all of that so we could start putting together the kinds of lists loved by those who
keep these kinds of statistics. We would like, for example, to be able to produce a listing of
all the members of a team sorted by their batting average. We have all the information that we
need, now that we can get the roster of players on a team, find out which of those players are
batters, and get their batting average. So we could dive in and start writing a sorting routine
for batting averages, or for any of the other statistics that we would want to use to rank the
various players.