Search code examples
pythonuml

UML: Can two objects of different classes have an aggregation relationship if neither of them contain's the other one?


How is A related to B according to UML?

from datetime import datetime

class B:
    def __init__(self, time) -> None:
        self.time = time

class A:
    def m(self):
        time = datetime.now()
        return B(time)

I'd say it classifies as an association. However im not sure if it also classies as an aggregation, since instances of A do not contain instances of B in any way.


Solution

  • It's neither. There is only a dependency from A to B (and datetime). A only produces an instance of B and has no remembering (association). And it is especially no aggregation whatsoever.

    A composite aggregation would be, if one class has an association to another and is reponsible for its lifetime. Only one instance can hold the responsibility. Usually the semantic value of aggregation is too little to be modeled in most (though not all) cases.

    As @Christophe noted, you could make it a Usage (special form of dependency which renders the same way) and add the «Create» stereotype. See p. 678 of UML 2.5:

    A usage dependency denoting that the client classifier creates instances of the supplier classifier.