lundi 6 juin 2016

POO : collection whose one item is the main one

Suppose a User has many Cars. These cars only belongs to him.

You could represent this 2 ways :

A User entity with a cars collection and a main_car attribute, pointing to one in the collection. The business rule would be: a user can only have a main car belonging to him.

OR

A User entity, with a cars collection. One of these cars could have a Boolean is_main attribute. The business rule would be: Only 1 car belonging to a user could be defined as main.

The business rules would be enforced in the setters.

I would like to have your advises and the pros and cons of the 2 approaches. Thanks !

Aucun commentaire:

Enregistrer un commentaire