18

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.

ahj
  • 745
  • 1
  • 6
  • 13

4 Answers4

21

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

Uffe
  • 10,396
  • 1
  • 33
  • 40
  • Hi, could you elaborate more about the point "if the contained class is used as a member in two different classes, it cannot be a singleton."? – David Chen Mar 26 '18 at 02:26
  • @Uffe How to represent `Directed association`? This is the first time that I heard such a class relationship. – John Apr 19 '22 at 12:18
  • @Uffe Are attribute association and directed association the same thing? – John Apr 19 '22 at 12:21
  • @ahj Are attribute association and directed association the same thing? – John Apr 19 '22 at 12:22
5

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

Mike G
  • 746
  • 5
  • 19
  • I realised nobody had actually answer the title question! – Mike G Mar 01 '12 at 10:45
  • 1
    ...and after my last comment, it was still unanswered! I've updated my diagram and answered it properly. I misread `singleton relationship` as `singleton pattern`. @ahj – Mike G Mar 06 '12 at 14:50
1

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

Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
1

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:

Mike G
  • 746
  • 5
  • 19
tereško
  • 58,060
  • 25
  • 98
  • 150