使用递归关系或“猪耳朵”在类图中。

huangapple go评论85阅读模式
英文:

Using recursive relationships or “pig ears ” in a class diagram

问题

[![我的UML图示][2]][1]

老师注意到这个部分有一个环,建议使用“猪耳朵”
关于这个问题我找不到太多信息或示例。
下面是无处不写的唯一一件事情:
>自反关系,有时称为递归关系或“猪耳朵”(因为在图示中这种关系的形象看起来像猪的耳朵 - 如果你稍微想象一下!)。请注意,这种关系总是可选的;否则,从定义上来说就是一个无限的层次结构。当我们开始研究无效和无法实现的数据结构时,我们稍后会看到这个规则。

也许有人可以通过示例解释如何使用“猪耳朵”,以及如何为它调整我的图示。

[1]: https://pastenow.ru/18223c3ca53e7b5f8011345c8b678188
[2]: https://i.paste.pics/18223c3ca53e7b5f8011345c8b678188.png
英文:

使用递归关系或“猪耳朵”在类图中。

Teacher noticed that there is a ring in this part, and advised to use "pig's ear"
I couldn't find much information or examples regarding this.
Here is the only thing that is written everywhere
>reflexive relationships, which are sometimes called recursive relationships or "pig ears" (because the image of such a relationship in the drawing looks like a pig's ear — if you use a little imagination!). Note that this relationship is always optional; otherwise, it would be by definition an infinite hierarchy. We'll look at this rule later when we start studying invalid and unrealizable data structures.

Perhaps someone can explain with examples how to use "pig's ear" and how to change my diagram for it.

答案1

得分: 3

你的老师显然被图表误导了。即使稍微想象一下,我也很难找到一个真正合理的反身类(对于任何能够提出一个在这里未提及的合理案例的人,+1)。

第一个建议:尝试使用共享目标将概括归为同一类。在这里,共享目标将是User

第二个建议:然后将所有共享属性移动到共享目标上。在你的图表中,人们会觉得学生和家长都有名字,但教职员工和管理员没有。

第三个建议:在图表中不要开始添加实现关联的属性。至少,在你的模型变得非常完善之前不要这样做。例如,在你的情况下,parent.pupilId 表示每个家长只有一个学生,但列表中却有多个。这很令人困惑。

第四个建议:在脑海中玩弄你的模型,看看是否符合实际情况。例如:

  • 一个家长可以在学校里有几个孩子(可以)
  • 一个孩子可以有几个带有账户的家长(不可以:你使用带有黑色钻石的合成,意味着每个孩子只属于一个家长)
  • 一个孩子可以有账户,但家长没有网络,也没有账户(不可以:合成还要求孩子账户的存在需要有一个家长账户)
  • 一个教师(员工)在学校有自己的孩子(不可以:因为员工没有孩子。解决方法:教师有一个员工账户,一个家长账户)(提示:如果你已经实现了系统,不想改变,你可以通过告诉客户这个缺陷是为了增加安全性,防止教师滥用授权改变自己孩子的成绩;-))
  • 管理员也是一样的(但是如果你展示了前面的缺陷而不是修复它,现在你就麻烦了…)
  • 管理员是员工(不可以,除非使用两个账户)
  • 一个家长也可以是学生(不可以)(你不相信吗?我的孩子去了我镇上的音乐学校。我觉得那不错,所以我自己也成了一个学生,拼命地想学吉他)。

第五个建议:当疑虑时,优先选择组合而不是继承。但只有在你绝对确定组合对象(在钻石另一侧)是独占所有并且不能没有组合体存在时,才使用组合(在这里,我们不是在谈论子女和父母,而是子女账户和家长账户)。

应用了所有这些原则之后,你的模型可能会更接近这个样子:

使用递归关系或“猪耳朵”在类图中。

交叉检查:你可以对上述所有情况回答"是"。

现在,如果你想取悦你的老师,你可以不再使用从用户到角色的一对多关系,而是可以进行一对0..1的组合,并在一个角色与0..1其他角色之间添加一个反身组合,从而创建一个角色链。

最后一个建议(今天):不要使用这个“猪耳朵”术语。它是模糊的(+1 是为了你已经做了研究),它不常用(好吧,我听说过“耳朵”和“米奇老鼠耳朵”),它会引起偏见,让人去寻找第二只耳朵,最重要的是,它会产生情感偏见(我认为它是消极的,但这种印象可能是文化上的)。

英文:

Your teacher was certainly mislead by the graphic. Even with some imagination, I have trouble to find a really justified reflexive class her (+1 for anybody answering with a plausible case which is not mentioned here).

First advice: try group the generalizations to the same class using a shared target. Here the shared target would be User,

Second advice: then show move all the shared properties to the shared target. In you diagram, one would have the impression that pupil and parent both have names, but that staff and admin have not.

Third advice: never start to add properties that implement associations in your diagram. At least, not before your model is bullet-proof. In your case for example, parent.pupilId suggests that there is only one pupil for a parent, whereas the list indicates several. This is confusing.

Fourth advice: play mentally with your model to see if it matches the case of real life. For example:

  • A parent can have several children in the school (ok)
  • A child can have several parents with an account (not ok: you use composition with a black diamond, which means that every child belongs to only one parent)
  • A child can have an account, but parents don't have internet and have no account (not ok: composition also requires that there is a parent account for the child account to exist).
  • A teacher (staff) has own children in school (not ok: because because staff has no children. Workaround: the teacher has one account as staff, and one account as parent)(tip: if you have already implemented your system, and do not want to change, you can feature this flaw by telling your customer that it's to increase security and avoid that the teacher changes the grades of its own children by abusing authorizations 使用递归关系或“猪耳朵”在类图中。 )
  • The same for admin (but uif you featured the previous flaw instead of fixing it, you are now screwed...)
  • An admin is staff (not ok, unless two accounts are used).
  • A parent may also be a pupil (not ok) (you don't believe me? My kids went to the music school of my town. I found it nice, so I myself became a pupil, desperately trying to learn guitar).

Fifth advice: when in doubt, prefer composition over inheritance. But also use composition only if you are absolutely certain that composed object (on the other side of the diamond) are exclusively owned and cannot exist without the composite (and here, we are not speaking of children and parents, but children accounts adn parent accounts).

After applying all these principle, you'd probably get a model closer to this one:

使用递归关系或“猪耳朵”在类图中。

Cross-check: you can answer yes on all the cases above.

Now, if you want to please your teacher, instead of a one-to-many relation from user to role, you can do a one to 0..1 composition, and add a reflexive composition between one role and 0..1 other roles, creating a chain of roles.

Last advice(for today): don't use this "pig ear" term. It's ambiguous (+1 for having done your research), it's not commonly used (ok, I already heard "ear", and "mikey-mouse ear"), it gives a bias by encouraging to look for a second ear, and last not least, it creates an emotional bias (I see it negative, but that impression might be cultural).

huangapple
  • 本文由 发表于 2020年10月12日 23:50:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64321129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定