I'm building a sports product. I have 3 classes
class Team {
getName // ex: Los Angeles Lakers
getShortName // ex: Lakers
getAbbrName // ex: LAL
}
class Match {
Team getHomeTeam
Team getAwayTeam
Play[] plays
}
class Play {
Match getMatch
String description // "Kobe Bryant scores a 3 pointer!"
}
A team is just any sports team. A match is a sports match between two teams. During a match, plays happen that get attached to that match.
I have the need to get the home/away team's name, short name, and abbreviated names, given a Match
or Play
. Which option do you prefer and why?
Option 1 - callers need to do this. Ex:
class SomeCaller {
foo() {
Play play = // somehow get a play;
Match match = play->getMatch;
Team home = match->getHomeTeam;
Team away = match->getAwayTeam;
String homeTeamName = home->getName;
String homeTeamShortName = home->getShortName;
String homeTeamAbbrName = home->getAbbrName;
String awayTeamName = away->getName;
String awayTeamShortName = away->getShortName;
String awayTeamAbbrName = away->getAbbrName;
// do something with the team names
}
}
Option 2 - Add the same methods to both classes
class Match {
Team getHomeTeam
Team getAwayTeam
Play[] plays
String getHomeTeamName() {
Team homeTeam = getHomeTeam();
return homeTeam->getName();
}
// same as above...
String getHomeTeamShortName()
String getHomeTeamAbbrName()
String getAwayTeamName()
String getAwayTeamShortName()
String getAwayTeamAbbrName()
}
class Play {
Match getMatch
String getHomeTeamName() {
Match match = getMatch;
return match->getHomeTeamName();
}
// same as above...
String getHomeTeamShortName()
String getHomeTeamAbbrName()
String getAwayTeamName()
String getAwayTeamShortName()
String getAwayTeamAbbrName()
}
Keep in mind I would want to get name, short name, abbr name, for both home and away teams given a Match or Play object, so there's going to be lots of method duplication with option 2.
Aucun commentaire:
Enregistrer un commentaire