Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a Singleton relationship in a class diagram

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

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

like image 954
ahj Avatar asked Feb 29 '12 11:02

ahj


People also ask

How do you represent a singleton in UML?

It is explained later, but an implication of the pattern is that there is only one instance of a class instantiated—never two. In other words, it is a “singleton” instance. In a UML diagram, such a class can be marked with a '1' in the upper right corner of the name compartment.

How do you write relationships in class diagram?

To show a composition relationship in a UML diagram, use a directional line connecting the two classes, with a filled diamond shape adjacent to the container class and the directional arrow to the contained class.

How do you identify a singleton class?

We can distinguish a Singleton class from the usual classes with respect to the process of instantiating the object of the class. To instantiate a normal class, we use a java constructor. On the other hand, to instantiate a singleton class, we use the getInstance() method.


3 Answers

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.

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.

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.

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.

enter image description here

like image 94
Uffe Avatar answered Oct 01 '22 04:10

Uffe


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):

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

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

like image 41
Mike G Avatar answered Oct 01 '22 04:10

Mike G


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).

like image 1
Michel Keijzers Avatar answered Oct 01 '22 06:10

Michel Keijzers