0

I have to write an Application for Android and I'm asking myself if there are better ways to design my app.

My problem: I have to create Groups which include Person. In my first Activity I want to show all the Groups the Person is part of. When I click on a Group I want to show all the Persons who are part of this group.

Question: So do I need a M:N relationship? Is this a bad design and are their better ideas to design the app?

public class Person {
private List<Group> allGroups
}


public Group {
private List<Person> allGroupMembers
}

I have already searched about 30 minutes but couldn't find an answer to my question.

I'm looking forward to your help.

  • If you want to show all the groups a person is part of, and all the persons in a group, then it seems only natural that your `Person` class should provide a method that returns a list of groups, and your `Group` class should provide a method that returns a list of persons. I'm not clear on why you would think "OOP" might suggest that you do something different. – ajb Jul 02 '17 at 01:18
  • But you do have something similar [here](https://stackoverflow.com/questions/1103693/how-to-model-a-many-to-many-relationship-in-code) – Isac Jul 02 '17 at 01:48
  • @Isac yes that is the same answer as from karim. It was very helpful. Thanks :) – Pieter Schmidt Jul 02 '17 at 02:54

2 Answers2

1

You should create three classes :

public class Person {
private int id_person; (primary key)
....
}


public class Group {
private int id_group; (primary key)
......
}

public class PersonGroup {
private int id_person_group (primary key)
private int id_person
private int id_group
....
}

PersonGroupe represent the relationship between person and group.

So if a person A exist in a Groupe X you should have an instance of PersonGroup Class

So if you want to get all the Groups the Person X is part of. you use this query (

Select * from Group
Join PersonGroupe on PersonGroupe.id_group = Group.id_group
where PersonGroupe.id_person = X.id_person (id_person of X)

if you want to get all persons the Groupe Y is part of :

Select * from Person
Join PersonGroupe on PersonGroupe.id_person = Person.id_person
where Groupe.id_group = Y.id_group (id_group of Y)
Karim
  • 322
  • 1
  • 10
  • Why are you providing SQL. The OP has not made any mention of a relational database - just OOP. – Michael Markidis Jul 02 '17 at 01:29
  • I know that..but i would just explain design app in sql. because it's easy to understand it with relational database.otherwise the three classes are enough to solve your problem. – Karim Jul 02 '17 at 01:44
  • 1
    Personally, I really appreciated the fact that you were using SQL for helping the asking user to gain a better perspective. –  Jul 02 '17 at 01:59
  • 1
    @MichaelMarkidis Isn't the reason for SQL obvious? When I see m:n then I automatically think of a database structure. It's not always about what a user asks, but about which details can help her/him have a better perspective of the solution. Sometime you don't know how or what to ask. Then it can be, that others still give you the right answer and maybe show you what, or how was the correct question. –  Jul 02 '17 at 02:07
  • 1
    Yes it was a great help. Thank you – Pieter Schmidt Jul 02 '17 at 02:50
0

Using a child List in each class or a separate JOIN class is a matter of opinion.

The biggest issue you have to resolve with a List in each class is circular dependencies.

duffymo
  • 305,152
  • 44
  • 369
  • 561