简体   繁体   English

UML中的类图正确关系吗?

[英]Is correct relationships of class diagram in UML?

The image shows the logistics of the Warehouse. 该图显示了仓库的物流。 Very very simplistic. 非常非常简单。 What is its concept: There are documents: ReceivingWayBill , DispatchingWaybill , ReplacementOrder . 它的概念是什么:有文档: ReceivingWayBillDispatchingWaybillReplacementOrder

They interact with the main classes: Warehouse , Counterparty , Item . 他们与主要类别互动: WarehouseCounterpartyItem

And the Register class: ItemRemainsInWarehouse . Register类: ItemRemainsInWarehouse It turns out, the document is confirmation of the operation, reception, sending, and so on. 事实证明,文档是对操作,接收,发送等的确认。 The Register simply stores information about the number of remaining goods. Register仅存储有关剩余货物数量的信息。

If you miss a lot of problems of this scheme, such as: the lack of generalization, getters and setters and a heap of everything else. 如果您错过了该方案的许多问题,例如:缺乏通用性,获取器和设置器以及其他所有内容。

Who can tell: the relationship between classes, and there is concrete aggregation everywhere, are placed correctly, or can we somehow consider the association in more detail? 谁能说:类之间的关系以及到处都有具体的聚合,是否放置正确,或者我们可以以某种方式更详细地考虑关联?

类图

Aggregation is evil ! 聚集是邪恶的

Read the UML specs about the two variants they introduced (p. 110): 阅读有关它们引入的两个变体的UML规范(第110页):

none : Indicates that the Property has no aggregation semantics. none :指示该属性没有聚合语义。 [hear, hear!] [听见,听见!]

shared : Indicates that the Property has shared aggregation semantics. shared :表示该属性具有共享的聚合语义。 Precise semantics of shared aggregation varies by application area and modeler. 共享聚合的精确语义因应用程序区域和建模器而异。

composite : Indicates that the Property is aggregated compositely, ie, the composite object has responsibility for the existence and storage of the composed objects (see the definition of parts in 11.2.3). Composite :指示Property是复合聚合的,即,复合对象负责组成对象的存在和存储(请参见11.2.3中各部分的定义)。

Composite aggregation is a strong form of aggregation that requires a part object be included in at most one composite object at a time. 复合聚合是聚合的一种强大形式,它要求一次将一个对象最多包含在一个复合对象中。 If a composite object is deleted, all of its part instances that are objects are deleted with it. 如果删除复合对象,则随即删除作为对象的所有零件实例。

Now, that last sentence clearly indicates where you should use composite (!) aggregation: in security related appications. 现在,最后一句话清楚地表明了在安全相关应用程序中应在何处使用复合(!)聚合: When you delete a person record in a database you need to also delete all related entities. 在数据库中删除人员记录时,还需要删除所有相关实体。 That often used example with a car being composed of motor, tires, etc. does not really fit. 在汽车由电动机,轮胎等组成的汽车中,这种经常使用的示例并不适合。 The tires do not vanish when you "delete" the car. 当您“删除”汽车时,轮胎不会消失。 Simply because you can not delete it. 仅仅是因为您无法删除它。 Even worse is the use of a shared composite since it has no definition per definition (sic!). 更糟糕的是使用共享组合,因为它没有每个定义的定义(原文如此!)。

So what should you do? 那你该怎么办? Use multiplicities! 使用多重性! That is what people usually want to show. 这就是人们通常想要展示的东西。 There are 0..n , 1 , etc. elements related to to the class at the other side. 在另一侧有0..n1等与该类相关的元素。 Eventually you name these by using roles to make it explicit. 最终,您通过使用角色来明确地命名它们。

If you consider DispatchingWaybill and ReceivingWaybill it looks like those are association classes. 如果考虑使用DispatchingWaybillReceivingWaybill则它们看起来像是关联类。 With the right multiplicities ( 1-* / *-1 ) you can leave it this way. 使用正确的多重性( 1-* / *-1 ),您可以通过这种方式保留它。 ( Edit : note the little dots at the association's ends which tell that the class at the opposite has an attribute named after the role.) 编辑 :请注意关联两端的小点,这些小点告诉相对的类具有以角色命名的属性。)

在此处输入图片说明

Alternatively attach either with a dashed line to an association between the classes where they are currently connected to. 或者,用虚线将它们附加到当前连接的类之间的关联上。

在此处输入图片说明

It is so hard (maybe impossible) to correct your whole model with provided explanation. 很难(可能无法)通过提供的说明来校正整个模型。 I give some improvements. 我做了一些改进。

  1. You should put Multiplicity of you relationships. 您应该放置多重关系。 They are so important. 它们是如此重要。 In some relationship, you have 1 ( ReplacementOrder , Warehouse ) and some of your relatioships are maybe * ( Item , ReceivingWayBill ) 在某种关系中,您有1个( ReplacementOrderWarehouse ),某些关系可能是*( ItemReceivingWayBill

  2. You put Aggregation between your classes and we know that Aggregation is type of Association. 您将聚合放在您的班级之间,我们知道聚合是关联的类型。 You can put Associations too. 您也可以放联想。 You can find a lot of similar questions and answers that explain differences between Association and Aggregation (and Composition). 您可以找到很多类似的问题和答案,这些问题和答案解释了关联和聚合(和构成)之间的差异。 see Question 1 , Question 2 and Question 3 . 参见问题1问题2问题3 But I recommend this answer . 但是我推荐这个答案

I think, there is NOT a very significant difference between Aggregation and Association . 我认为, AggregationAssociation之间并没有太大的区别 See my example in this question . 请参阅我在这个问题中的示例。

Robert C. Martin says (see here ): 罗伯特·马丁(Robert C. Martin)说(请参阅此处 ):

Association represents the ability of one instance to send a message to another instance. 关联表示一个实例向另一个实例发送消息的能力。

Aggregation is the typical whole/part relationship . 聚集是典型的整体/部分关系 This is exactly the same as an association with the exception that instances cannot have cyclic aggregation relationships (ie a part cannot contain its whole). 这与关联完全相同,但实例不能具有循环聚合关系(即,一部分不能包含其整体)。


Therefor : some of your relationships are exactly an Aggregation. 因此 :您的某些关系恰好是一个集合。 (relationship between Item and other classes). Item与其他类之间的关系)。 Your Counterparty has not good API definition. 您的Counterparty没有良好的API定义。 Your other relationships is about using Warehouse class. 您的其他关系是关于使用Warehouse类。 I think (just guess) the other classes only use Warehouse class services (public methods). 我认为(只是猜测)其他类仅使用Warehouse类服务(公共方法)。 In this case, they can be Associations. 在这种情况下,它们可以是协会。 Otherwise, if they need an instance of Warehouse as a part, they are Aggregations. 否则,如果他们需要一部分Warehouse实例,则它们就是聚合。

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

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