I want to know if the below relationship model between classes can be improved. I am using MongoDB as a database layer.
public class TodoItem {
private String description;
private Person person;
}
public class Person {
private List<TodoItem> items;
}
Again my question is specific to the design of classes where relationship like One-to-Many and Bi-Direction is involved. Thanks.
It really depends on your requirements/environment.
e.g. you have bidirectional dependencies. They are trouble some to maintain, but nice for the client using this. What is more important? Do you really need to navigate both ways more or less equally often?
You store full objects. Maybe you should store proxies instead or id’s and a repository. Nobody can tell until we know about your requirements, both functional and non functional.
Actually if you don’t have requirements, the design is wrong, because you shouldn’t have a class if it doesn’t help fulfilling a requirement.
There is no absolute right or wrong in software design. If such a decision would be possible we wouldn’t need to make it manually, it would be available as a language feature in every modern language. Design is about understanding requirements and understanding the effects design decision have on the degree of fulfillment of those requirements.
So if you want to learn about design try the following:
Take simple functional requirements and think about how your design changes with changing non functional requirements: it has to run on a chip card; it has to process thousands requests per second. Lots of writes, few reads; Lots of reads, few writes. …