Let's say I have the following relationship:
class A {
public:
A(B& _objB);
B& objB;
};
A::A(B& _objB)
: objB(_objB) {}
This doesn't seem to fit with my understanding of the UML definitions of aggregation or composition. It feels like a composition, but in a composition the lifetime of the owned object should be tied to the parent object. In this case objB exists before A is created and after A is destroyed. A can't live without B, but B can live without A. This is the reverse of the standard composition relationship. Does that make it an aggregate or something else?
Aggregation relationship is also a "has-a" relationship. The only difference between Aggregation and Composition is that in Aggregation, objects are not tightly coupled or don't involve owning. All the objects are independent of each other and can exist even if the parent object gets deleted.
Aggregation implies a relationship where the child can exist independently of the parent. Example: Class (parent) and Student (child). Delete the Class and the Students still exist. Composition implies a relationship where the child cannot exist independent of the parent.
In UML models, an aggregation relationship shows a classifier as a part of or subordinate to another classifier. An aggregation is a special type of association in which objects are assembled or configured together to create a more complex object.
Aggregation (also known as **containment) is a specialised form of association. It defines a one-way relationship that specifies a 'has-a' relationship between two classes. For example, you can say that a wallet has money. However, the money does not need a wallet in order to exist, so the relationship is one-way.
Already answered here , the answer is aggregation. a none owning usage of B.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With