1

I have 3 classes, Team, Game and Round.

Game contains a list of Teams.

Round contains a list of Game.

I'm going to create multiple objects from each class.

Should I create another classes for this like which contain a list of each object:

GameRepository
TeamRepository
RoundRepository

Or should I just keep the collections in their own classes?

5
  • 2
    Java offers a lot of Collections. Use them as is.
    – Aubin
    Mar 5, 2018 at 19:10
  • So I should keep a list of games in the class Game for example?
    – sander
    Mar 5, 2018 at 19:16
  • 1
    @sander Consider it from the business logic point of view. Do you have Game as an entity? Then yes. Do you just happen to have lists of Teams once in a while? Then probably no.
    – lexicore
    Mar 5, 2018 at 19:21
  • I guess it should be: Game contains a list of Teams. AND Game contains a list of Rounds.
    – The Guest
    Mar 5, 2018 at 19:26
  • What would you use the repositories for? It all depends on what you want to achieve with all of this. Mar 5, 2018 at 19:27

2 Answers 2

2

Remember the single responsibility principle.

I am sure your classes already have a plenty of methods to manage/control the inner state. Do they need another responsibility to take? No, they don't.

Storing items, being a collection is a completely new responsibility and it shouldn't be messed up with the already-given ones.


On the other hand, there is nothing wrong if a Round will contain a List<Game> and a Game will have references to Teams. But you need to be sure that, for example, removing a Game wouldn't affect Team instances. The issue is still to decide which class keeps instances, and which one just refers them.

1
  • 1
    So going with the repository would be better and cleaner code I guess.
    – sander
    Mar 5, 2018 at 19:24
1

If I have to do that, I would do it this way(These classes can be in different files):

public class Game{
List<Team>teamList;
}

public class Round{
 List<Game>gameList;
}

public class Team{

}

That way if you have an instance of a Round, you have everything you need

1
  • Isn't this just repeating what the OP was asking?
    – dub stylee
    Mar 5, 2018 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.