`

difference aggregation and association

阅读更多
Consider the distinction between object aggregation and acquaintance and how differently they manifest
themselves at compile- and run-times. Aggregation implies that one object owns or is responsible for
another object. Generally we speak of an object having or being part of another object. Aggregation implies
that an aggregate object and its owner have identical lifetimes.
Acquaintance implies that an object merely knows of another object. Sometimes acquaintance is called
"association" or the "using" relationship. Acquainted objects may request operations of each other, but they
aren't responsible for each other. Acquaintance is a weaker relationship than aggregation and suggests much
looser coupling between objects.
In our diagrams, a plain arrowhead line denotes acquaintance. An arrowhead line with a diamond at its base
denotes aggregation:
It's easy to confuse aggregation and acquaintance, because they are often implemented in the same way. In
Smalltalk, all variables are references to other objects. There's no distinction in the programming language
between aggregation and acquaintance. In C++, aggregation can be implemented by defining member
variables that are real instances, but it's more common to define them as pointers or references to instances.
Acquaintance is implemented with pointers and references as well.
Ultimately, acquaintance and aggregation are determined more by intent than by explicit language
mechanisms. The distinction may be hard to see in the compile-time structure, but it's significant.
Aggregation relationships tend to be fewer and more permanent than acquaintance. Acquaintances, in
contrast, are made and remade more frequently, sometimes existing only for the duration of an operation.
Acquaintances are more dynamic as well, making them more difficult to discern in the source code.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics