C++ 如何在类图中显示单例关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9497968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:53:53  来源:igfitidea点击:

How to show a Singleton relationship in a class diagram

c++singletonumlclass-diagram

提问by ahj

If a class contains pointer to a singleton class, can it beaggregation?

如果一个类包含指向单例类的指针,它可以是aggregation吗?

To my understanding it cannot be a has-arelationship since the class does not make an instance of the singleton class, it is just using it like associationrelationship.

据我了解,它不能是has-a关系,因为该类没有创建单例类的实例,它只是像association关系一样使用它。

回答by Uffe

The title doesn't make 100% complete sense as written. There are singleton classes, but there aren't really singleton relationships. Any relationship can be assigned a multiplicity at either end, so if you mean one-to-one relationships, all you do is assign multiplicity 1 at both ends.

标题并不像书面的那样 100% 完全有意义。有单例类,但没有真正的单例关系。任何关系都可以在任一端分配多重性,因此如果您指的是一对一关系,您所做的就是在两端分配多重性 1。

Classes can also have multiplicities. You don't often see this used, except in one case: singletons.

类也可以有多重性。你不经常看到这种用法,除了一种情况:单身。

When it comes to A having or containing or referencing B, there are basically three levels of tightness in UML.

当涉及到 A 具有或包含或引用 B 时,UML 中基本上有三个级别的紧密度。

Aggregation (unfilled rhomboid arrow head) implies that the containment is not exclusive and that the contained object does not share any aspect of its lifecycle with the containing object. In implementation, this is typically a pointer.

聚合(未填充的菱形箭头)意味着包含不是排他的,并且包含的​​对象不与包含对象共享其生命周期的任何方面。在实现中,这通常是一个指针。

Composition (filled rhomboid arrow head) implies that the contained object gets destroyed when the containing object does. Getting this to work usually means that the containment is exclusive. In implementation, this is often a pointer whose destructor is called in the destructor of the containing class (although it may not be created in the constructor of the containing class).

组合(填充菱形箭头)意味着当包含对象销毁时,包含的对象会被销毁。让它发挥作用通常意味着遏制是排他性的。在实现中,这通常是一个指针,其析构函数在包含类的析构函数中调用(尽管它可能不在包含类的构造函数中创建)。

Directed association or member attribute (same thing in UML) implies that the contained object is part of the state, or a constituent if you will, of the containing object. In implementation, this typically means that the reference is not a pointer, or if it is that the contained object is co-created and -destroyed with the containing object.

定向关联或成员属性(在 UML 中相同)暗示所包含的对象是包含对象的状态的一部分,或者是包含对象的组成部分。在实现中,这通常意味着引用不是指针,或者如果它是包含对象与包含对象共同创建和销毁。

Aggregation of a singleton is perfectly permissible (even from several different classes), because aggregation is by definition non-exclusive.

单例的聚合是完全允许的(即使来自几个不同的类),因为聚合根据定义是非排他的。

Composition is a bit iffy, unless the containing class is also a singleton.

组合有点不确定,除非包含类也是单例。

Attribute / directed association is most likely wrong. If the containing class is a singleton, it doesn't make any sense to make the contained class a singleton as well since that's implied. And if the contained class is used as a member in two different classes, it cannot be a singleton.

属性/定向关联很可能是错误的。如果包含类是单例,则将包含的类也设为单例没有任何意义,因为这是隐含的。如果包含的类在两个不同的类中用作成员,则它不能是单例。

In addition to the above, you can also of course add as many Usage relationships as you wish. This is common in all design, and implies that the class at the source end of the relationship calls methods in the class at the target end.

除了上述之外,您当然还可以根据需要添加任意数量的 Usage 关系。这在所有设计中都很常见,意味着关系源端的类调用目标端类中的方法。

enter image description here

在此处输入图片说明

回答by Mike G

I would say, technically, yes, you can have a member variable that is a pointer to a singleton object and call it aggregation; using the aggregation term doesn't have much meaning once you write the code though. For all intents and purposes, it is just an association.

我会说,从技术上讲,是的,您可以拥有一个指向单例对象的指针的成员变量,并将其称为聚合;但是,一旦您编写了代码,使用聚合术语就没有太大意义。出于所有意图和目的,它只是一个关联。

However, the use of an aggregation association in a diagram may or may not help a viewer of the diagram to comprehend it. That probably depends on who you are going to show it to and what they might understand aggregation to mean.

然而,在图表中使用聚合关联可能会也可能不会帮助图表的查看者理解它。这可能取决于您要将其展示给谁以及他们可能会理解聚合的含义。

To actually answer the question in the title (using info from The UML User Guide (2nd Edition):

要实际回答标题中的问题(使用来自 The UML User Guide (2nd Edition) 的信息):

______________________          ______________________
|                    |          |                   1|
|  AggregatingClass  |          |   SingletonClass   |
|____________________|      0..1|____________________|
|                    |<>--------|                    |
|____________________|          |____________________|
|                    |          |                    |
|____________________|          |____________________|

(Note the 1in the upper right hand corner of the singleton class box indicating cardinality.)

(注意1单例类框右上角的 指示基数。)

回答by Michel Keijzers

There is also an aggregation with an open square instead of a filled. The open means the first instance does not make the other (but still has a has-a relationship).

还有一个带有开放正方形而不是填充的聚合。开放意味着第一个实例不会生成另一个(但仍然具有 has-a 关系)。

回答by tere?ko

You could pretend that it is aggregation, but truth of it is this: singletons have nothing to do with object oriented code. They are a form of global state (just like global variables are).

你可以假装它是聚合,但事实是这样的:单例与面向对象的代码无关。它们是全局状态的一种形式(就像全局变量一样)。

You might want to consider a different approach to the problem.

您可能需要考虑采用不同的方法来解决问题。

P.S some materials that could help:

PS一些可以帮助的材料: