0

This question is maybe opinion based but i am wondering which principle should i use. Here is my situation.

I have a class named TravelOffer. This class looks like this:

public class TravelOffer
{
    private final long id;

    private final StartZone startZone;
    private final EndZone endZone;
    private final List<Waypoint> waypoints;

    private final Period startOff;

    private final User travelCreator;
    private final List<User> participantsList;

    private final TravelExtras travelExtras;

    private final CandidateApprovement candidateApprovement;

    private int numberOfSeats;

    private double pricePerPerson;

    private Luggage luggage;

    private boolean additionalLuggageTransport;

    private String additionalDescription;
}

As u see there are the following attributes:

User tavelCreator and List<User> participantsList

What i am wondering is should it be like this or simply to replace the User object with an String userId and then from a singleton class to get the User object which will be in a Hashmap.

Any ideas?

Joro Seksa
  • 1,525
  • 3
  • 18
  • 44

2 Answers2

1

I don't think that storing IDs instead of references is a good idea. It would make the code more complicated because every time you need a User associated to the TravelOffer you have to call your let's say DAO which returns the reference. You don't have to bother about the memory as long as Java uses references. Other thing: participants perfectly fit into TravelOffer so it shouldn't be split.

Compare that:

travelOffer.getTravelCreator().getName()
userDao.getUser(travelOffer.getTravelCreatorId()).getName()

First one looks better for me.

Is your question related to object-relation mapping ?

g-t
  • 1,455
  • 12
  • 17
1

Does participantsList and travelCreator logically fit into TravelOffer ? If yes, there is no point in decoupling Traveloffer and User by briding it through userId. One of the few situations where an id approach would be preferred is if we truly require loose coupling (say when we need to do a lookup from another system based on Id) which would imply that TravelOffer does not need User which is not the case here. If both TravelOffer and User are part of the same system, its better to hold reference to the object instead of id.

user1339772
  • 783
  • 5
  • 19