有效设计类之间的关系
我想知道下面的类之间的关系模型是否可以改进。我使用 MongoDB 作为数据库层。
public class TodoItem {
private String description;
private Person person;
}
public class Person {
private List<TodoItem> items;
}
同样,我的问题是特定于涉及一对多和双向等关系的类的设计。谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这实际上取决于您的要求/环境。
例如,您有双向依赖性。它们维护起来很麻烦,但对于使用它的客户来说却很好。更重要的是什么呢?您真的需要或多或少地同等频率地进行双向导航吗?
您存储完整的对象。也许您应该存储代理或 ID 和存储库。在我们了解您的需求(功能性和非功能性)之前,没有人能告诉我们。
实际上,如果你没有需求,那么设计就是错误的,因为如果它不能帮助满足需求,你就不应该有一个类。
软件设计没有绝对的对错。如果这样的决定是可能的,我们就不需要手动做出它,它将作为每种现代语言的语言功能提供。设计是关于理解需求并理解设计决策对这些需求的满足程度的影响。
因此,如果您想了解设计,请尝试以下操作:
考虑简单的功能需求,并考虑您的设计如何随着非功能需求的变化而变化:它必须在芯片卡上运行;它每秒必须处理数千个请求。写多,读少;读的多,写的少。 ...
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. ...
您真的需要双向关联吗?例如,部门是否必须知道它属于哪家公司?
通过单向关联,您的耦合度较低,
Department
不必了解有关Company
的任何信息(或者如果Company
发生变化,也无需进行更改)。Project
、Employee
也是如此,...没有更多信息,这就是我所看到的。
Do you really need to have bidirectionnal associations? For example, does Department has to know to which Company it belongs?
With a unidirectionnal association you have less coupling,
Department
doesn't have to know anything aboutCompany
(or change ifCompany
changes). Same goes forProject
,Employee
, ...Without more informations that's all I see.
简单的答案是
Simple answer would be