A method should be in the class which realises a action or in the realised class?

Tags: ,



Maybe the title is a little confusing, but I didn’t know how to say it properly (my English is not good at all, so sorry if it is harder to understand).

Let’s say we got 2 classes, Person and Group, and a person can join a group. Both got a Collection of the joined groups or the persons that already joined the group.

I want a method to allow me to do that. Would it better to have a method in the Person class which is joinGroup(Group g) or a method in the Group which is addPerson(Person p)? In any way the method have to do some checks and not only add the objects to the Collections.

I’m using Java but I think it’s not important as it’s more a design questions.

Answer

Not all person should join a group. That is, it is possible that a person may not be part of any group.
But a Group should have person.

So i think, Group Should have the method adddPerson(Person)



Source: stackoverflow