So I have a class called Game
and I need to retrieve several objects of Game
from the database based on their session ID, so I have this method:
public ArrayList<Game> getGamesBySession
Okay, but I have to pass a parameter there so I would know which session's games I want to retrieve and I have two possibilities. I can either pass an int
or I can pass an object of Session
which has an attribute id
and then use session.getID()
. In the end, both would result in the same thing basically, but what I am wondering is something else.
My question is, which is a better approach and if I pass an object, would it mean that Game
is high coupled with Session
and violates GRASP?
That depends, is session.ID just an int? If it is, I would rather pass the entire session in, since getGamesBySession(session)
is a lot clearer than getGamesBySession(int)
.
However, you could also consider wrapping the int into a SessionIdentifier
object, which gives a bit more semantic meaning to the value. getGameBySession(SessionIdentifier)
is equally as clear and would prevent you from passing in some random int from elsewhere...