简体   繁体   English

子类之间的组成

[英]Composition between child classes

Is it possible or reasonable to have composition relationship between child classes? 子类之间是否有可能构成关系?

For examples: 举些例子:

Staff has two childs, Waiter and Manager (Inheritance). 工作人员有两个孩子,侍者和经理(继承)。

Manager contains a list of Waiter (Composition). 管理器包含一个服务员(组成)列表。

Or is there a better way of representing their relationships? 还是有更好的方式表达他们的关系?

It is possible to have composition between child classes and sometime 'reasonnable' but in your case I would just model the relation between Waiter and Manager as an Association as de picted below. 子类之间的组合可能是有可能的,有时是“合理的”,但在您的情况下,我将服务员和经理之间的关系建模为协会,如下所示。 在此处输入图片说明

Yes, it is (syntactically) possible to have composition between ANY two classes. 是的,(在语法上)可以在任何两个类之间进行合成。 If it is reasonable or not, depends on every single case. 是否合理,取决于每种情况。

In your case it is definitelly not a good idea, because of the semantics behind the composition. 在您的情况下,由于组合背后的语义,这绝对不是一个好主意。

As the name says it, a composition means a Whole-Part kind of relationship , very strong one. 顾名思义, 构图是一种全部分的关系 ,非常牢固。 The "Whole" has a fuul control over the "Part", creates its instances, typically has them kind kind of hidden from the outside world and finally destroys them. “整个”对“零件”具有控制权,可以创建其实例,通常将其隐藏在外部世界中,并最终将其销毁。 "Part" most often than not does not exist out of context of the "Whole" and has no function besides its participation in the "Whole". “部分”通常不存在于“整个”的上下文之外,并且除了其参与“整个”之外没有任何功能。

Possible examples: House and Window, Invice and InvoiceItem, Body and Head, etc. 可能的示例:房屋和窗户,发票和发票项目,正文和标题等。

Note that even those examples could require different type of relationship in different contexts (for example, in a robot manufacturing software, Body and Head could be linked with some other relationship, as Head could exist independent of Body :)). 请注意,即使是这些示例在不同的上下文中也可能需要不同类型的关系(例如,在机器人制造软件中,Body和Head可以与某些其他关系链接,因为Head可以独立于Body存在)。

Back to your question, I would use a simple association between Manager and Waiter . 回到您的问题,我将使用Manager和Waiter之间的简单关联 The nature of this relationship is weaker, a Waiter can certainly live out of context of his Manager, Manager might have certain control over Waiter, but it probably does not control his lifecycle. 这种关系的性质较弱,服务员当然可以在其经理的上下文之外生活,经理可能对服务员有一定的控制权,但可能无法控制他的生命周期。

I suggest you too use relationship names that are as meaningful as possible. 我建议您也使用尽可能有意义的关系名称。

Manager contains a list of Waiter 经理包含服务员列表

is not very accurate and even misleading (that's why you were tempted to use composition in the first place). 不是很准确,甚至会引起误解(这就是为什么您很想使用合成的原因)。 Manager does not control Waiter, he might be "in charge of", or his boss or something. 经理不能控制服务员,他可能是“负责人”,也可能是老板或其他人。 Anyway, I prefer to use association roles, over then relationship names, as more formal. 无论如何,我更喜欢使用关联角色,而不是关系名称,因为它更为正式。

Something like this: 像这样:

在此处输入图片说明

Another possible relationship between Manager and Waiter is an aggregation . Manager和Waiter之间的另一个可能的关系是聚合 Its semantics is somewhere in between plain association and composition and often depicts a Group-Member kind of relationship. 它的语义介于简单关联和构图之间,通常描述了组成员关系。 In this case it might make sense but is definitelly a matter of personal taste. 在这种情况下,这可能有意义,但绝对取决于个人喜好。

2nd VERSION (see kiwiron's answer and comment(s). 第二版(请参阅kiwiron的答案和评论。

The idea is to separate between the personal and employment aspects of a Person . 这个想法是要在一个人的个人和雇用方面分开 Each Person have only one "current" Position, but it can be easily changed, even keeping the existing Positions and "reusing" them. 每个人只有一个“当前”职位,但可以轻松更改,甚至保留现有职位并“重用”它们。

This resolves the problem explained in the mentioned answer. 这样可以解决上述答案中解释的问题。

在此处输入图片说明

Which solution is the best in your case can only be determined by yourself. 哪种解决方案最适合您,取决于您自己。 :) :)

I think the basic class hiearchy is incorrect. 我认为基本课程是不正确的。 A waiter can become a manager, and vice-versa. 服务员可以成为经理,反之亦然。 This suggests that both Waiter and Manager are roles played by Staff and these roles are time-constrained . 这表明,这两个服务员和经理都是由工作人员扮演的角色和这些角色的时间限制 Inheritance should not be used under these conditions. 在这些情况下,不应使用继承。

I suspect that it is the manager role that has a list of waiters - not the manager staff member. 我怀疑是拥有一系列服务员的经理角色,而不是经理人员。 (ie if the manager resigns, the waiters will still be in the list for the replacement manager) (即,如果经理辞职,服务员仍会在替换经理的列表中)

Try googling "Peter Coad". 尝试使用Google搜索“ Peter Coad”。 His data modelling techniques have changed my way of looking at the data modelling world. 他的数据建模技术改变了我看待数据建模世界的方式。

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

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