Link to home
Create AccountLog in
Avatar of akohan
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:

    public class Team
    {
        private string teamName;
        public string TeamName
        {
            get { return teamName; }
            set { teamName = value; }
        }
    }

Open in new window

    public class Division
    {
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public List<Team> DivisionList;
    }

Open in new window



    public class League
    {
        private string Name;
        public string name
        {
            get { return name; }
            set { name = value; }
        }

        List<Division> DivisionList;
    }

Open in new window

Avatar of gt2847c
gt2847c
Flag of United States of America image

It captures the basic idea of the statement.  If you're simply looking for a very high level rough sketch, this may be sufficient.  It would not, however, be a good way to implement it as-is in a real program.

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...
Avatar of akohan
akohan

ASKER

Yes, critique is what I need.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of gt2847c
gt2847c
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of ambience
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.
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...