akohan
asked on
organization of classes base on a statement.
Hello,
I need some help on following statement. I need to create class(es) base on it:
ListLeagueList is a list of League objects. Each League object has a Name and a collection of Division objects. Each Division has a Name and a collection of Team objects,
and each Team object has a Name.
This is what I have done, please correct me if I'm wrong:
I need some help on following statement. I need to create class(es) base on it:
ListLeagueList is a list of League objects. Each League object has a Name and a collection of Division objects. Each Division has a Name and a collection of Team objects,
and each Team object has a Name.
This is what I have done, please correct me if I'm wrong:
public class Team
{
private string teamName;
public string TeamName
{
get { return teamName; }
set { teamName = value; }
}
}
public class Division
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public List<Team> DivisionList;
}
public class League
{
private string Name;
public string name
{
get { return name; }
set { name = value; }
}
List<Division> DivisionList;
}
ASKER
Yes, critique is what I need.
Thanks
Thanks
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
I do not agree with the point that League/Divsion should be struct - for one this is no C++. In managed code struct has overheads like boxing and therefore using a class is better. Use structs in managed code, only when there is a real need like say strict memory layout.
Also, even though League and Dvision look alike they are not the same. League has divisions - not teams and division does not have divisions - so it makes sense to keep then separate entities.
As a good practice, I would also recommend that you rather use the generic IList<T> interface, for example
public class Division
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
IList<Team> divisionList = new List<Team>();
public IList<Team> DivisionList
{
get { return divisionList;}
set { divisionList = value; }
}
}
The setter for DivisionList should be considered only when it really makes sense to have one because it reduces encapsulation.
Also, even though League and Dvision look alike they are not the same. League has divisions - not teams and division does not have divisions - so it makes sense to keep then separate entities.
As a good practice, I would also recommend that you rather use the generic IList<T> interface, for example
public class Division
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
IList<Team> divisionList = new List<Team>();
public IList<Team> DivisionList
{
get { return divisionList;}
set { divisionList = value; }
}
}
The setter for DivisionList should be considered only when it really makes sense to have one because it reduces encapsulation.
ambience, a couple of points...
I didn't recommend the use of a struct, simply said it was a possibility.
On the class structure for League and Division, I had considered trying to explain templating here given the duplicate form/functionality, but it is rather more advanced and would require a longer discussion. Upon further contemplation, I probably should just have left point three out of the discussion...
I didn't recommend the use of a struct, simply said it was a possibility.
On the class structure for League and Division, I had considered trying to explain templating here given the duplicate form/functionality, but it is rather more advanced and would require a longer discussion. Upon further contemplation, I probably should just have left point three out of the discussion...
To prevent unnecessary details (assuming you don't want them) I'll leave it here. If you would like a critique of the above, let me know and I'll post additional details...