Home arrow static arrow Java Programming [Archive] - Too many collections?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Too many collections?
This topic has 3 replies on 1 page.

Registered: 2/7/04
Too many collections?  
Aug 5, 2004 8:10 PM

Hello everyone! I would like to ask a question that is maybe related to architecture.

I am making a soccer db, which holds some information.

A part of the model is as follows (expressed in UML):

        1         1..*       1              1..*League ------------- Team ------------------ Player 

I suppose this model is correct. However I need a user interface where users will be able to see a list of the leagues the teams and the players. They will also be able to change the information.

This means that I have to have collection classes for each class in the system?

For instance for the league class, I need a Collection of Leagues that will hold/populate from the db the information of each separate league. I will also need a collection of teams in order to show the information on screen. This would make the model like this:

      1           1..*   1       1    1       1..*  1   1       1   1..*Leagues ------------League---------Teams--------Team ----Players------Player 

Are there any alternatives to this approach, or do I have to have collections of every class?

Thanks in advance for your answers!

Registered: 3/30/99
Re: Too many collections?  
Aug 5, 2004 8:20 PM (reply 1 of 3)

You could create a Teams class and a Players class and a Leagues class if you want. However, you don't have to. The 1..* relation already implies that, for instance, a league has multiple teams. Whether you create a Teams class to group the teams or just give the League a direct reference to a collection of teams (that is just, say a vanilla LinkedList) is up to you.

Personally, the only reason I'd create the Leagues, Teams, and Players classes is either a) if I were really paranoid about type safety or b) if I needed to add some functionality that's not present in a normal Collection or List. Otherwise, I'd go with a vanilla List and the simpler UML model. And for reason (a), if you're using 1.5, you can use generics and still keep your UML simple.

Registered: 2/7/04
Re: Too many collections?  
Aug 6, 2004 4:50 AM (reply 2 of 3)

Thank you very much for your answer jverd.

I also want to keep the model simple. I was going to implement 1-many relations with java collections (LinkedHashSet), since the classes are already created as you described by a CASE tool that I am using.

But it seems like it will need a lot of processing. For example in the first model if I want to display a list of all the players irrespective of the Team (all the players that exist in the system), I have to have a populateRelatedTeams function for each League (that will fetch the teams of the specific League) and if I call it for every League it will eventually fetch all of the teams in all of the Leagues. Then for every team I should have a populateRelatedPlayers that will fetch the players of the specific Team.
But this means a lot of select queries in the database.

The second option I think I have (if I do not use specific collections) is to create a method in the Team object which will get all the players, irrespective of the Team. Then all I have to do in my code is create a "dummy" Team object and call that method. This will only need a select statement from the db and I won't have to populate the whole class tree. But I have a feeling that I am mixing things if I do it this way. Or maybe not?

Registered: 98-02-20
Re: Too many collections?  
Aug 6, 2004 4:57 AM (reply 3 of 3)

You might just need a TableModel that will do different kinds of queries and return Value Objects to display the data. The objects you're describing don't sound like they have much behavior associated with them. You just want to do data display for information fetched from a database.

This topic has 3 replies on 1 page.