简体   繁体   English

了解如何 model UML 类/数据库

[英]Understanding how to model UML Class/Database

I'm confused with designing a client software with database integration to what should be a member variable of the class or just a query to the database.我对设计一个与数据库集成的客户端软件感到困惑,该软件应该是 class 的成员变量,或者只是对数据库的查询。 Let me be specific with a trivial example:让我用一个简单的例子来具体说明:

If I have, lets say, a Student class, which has a list of "friends" that are Student objects.比方说,如果我有一个Student class,它有一个作为Student对象的“朋友”列表。 Should my software design have an ArrayList<Student> as a member variable of the Student class or should the Database deal with the relationship itself and the Student class doesn't account for those "friends"?我的软件设计是否应该有一个ArrayList<Student>作为Student class 的成员变量,或者数据库应该处理关系本身并且Student class 不考虑那些“朋友”? How should a proper UML class-diagram be in this case?在这种情况下,正确的 UML 类图应该如何?

You need a one-to-many relationship between Student and friends in both the relational database and the object model.你需要在关系数据库和 object model 中建立一对多的Student和朋友关系。

This question is broader than you may think, as there are many ways to deal with it.这个问题比你想象的要广泛,因为有很多方法可以解决它。 Here some first ideas:这里有一些初步想法:

Let's start with a quick class diagram.让我们从快速的 class 图开始。 The friendship between students is a many-to-many association.同学之间的友谊是多对多的关联。
带有学生班级和与自身关联的图表

In a database, a many-to-many association is usually implemented using an association table .在数据库中,多对多关联通常使用关联表来实现 So you'd have two tables: STUDENTS and FRIENDSHIPS with pairs of ids of befriended students:因此,您将有两个表: STUDENTS和 FRIENDSHIPS,其中包含成对的好友 ID: 与双外键约束相关的两个表

To load a Student object from the database, you'd read the data in a STUDENTS row and use it to initialize your object. For the friendship, you'd have to read the relevant FRIENDSHIPS rows.要从数据库中加载Student object,您需要读取STUDENTS行中的数据并使用它来初始化您的 object。对于友谊,您必须读取相关的FRIENDSHIPS行。

But how to use these tables in the application?但是如何在应用程序中使用这些表呢?

  • A first possibility would be to load each Student friend and insert it in the ArrayList<Student> .第一种可能性是加载每个Student朋友并将其插入ArrayList<Student> But each loaded student is like the first student and could have oneself friends that you'd have to load as well, You'd end up loading a lots of students, if not all.但是每个加载的学生就像第一个学生,并且可能有自己的朋友,您也必须加载这些朋友,您最终会加载很多学生,如果不是全部的话。 just for getting the single one you're interested in.只是为了得到你感兴趣的那一个。
  • A second possibility would be use an ArrayList<StudentId> instead of an ArrayList<Student> and populate it.第二种可能性是使用ArrayList<StudentId>而不是ArrayList<Student>并填充它。 You'd then load the friends just in time, only when needed.然后你会及时加载朋友,只有在需要的时候。 But this would require some more important changes in your application.但这需要在您的应用程序中进行一些更重要的更改。
  • A third possibility is not to expose an ArrayList .第三种可能性是不公开ArrayList Not leaking the internals is always a good idea.不泄露内部总是一个好主意。 Instead use a getter.而是使用吸气剂。 So you'd load the friends only if student.getFriends() is called.因此,只有在调用student.getFriends()时才加载朋友。 This is a convenient approach, as you'd have a collection of friends at your disposal, but avoid being caught in a recursive loading of friends of friends.这是一种方便的方法,因为您可以随意使用一组朋友,但要避免陷入朋友的朋友的递归加载中。

In all the cases, you may be interested in using a repository object to get individual or collections of students, and encapsulate the database handling.在所有情况下,您可能有兴趣使用存储库object 来获取学生个人或 collections,并封装数据库处理。

Advice: as said, there are many more options, the repository is one approach but there are also active records, table gateways and other approaches.建议:如前所述,还有更多选择,存储库是一种方法,但也有活动记录、表网关和其他方法。 To get a full overview, you may be interested in Martin Fowler's book Patterns of Enterprise Application Architecture .要获得完整的概述,您可能对 Martin Fowler 的书Patterns of Enterprise Application Architecture感兴趣

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM