Class aggregation and attribute ratio

Asked

Viewed 247 times

4

Aggregation only occurs when a class A, which aggregates a class B, has an attribute of the type B?

There can be aggregation without necessarily having an attribute of the type of the other?

For example, if I instate an object of another class in a method of my class, but that instantiated object is not an attribute of my class, the relation is still of composition?

  • 1

    Here is a good explanation and not only of aggregation https://answall.com/questions/25619/composi%C3%A7%C3%A3o-e-agrega%C3%A7%C3%A3o-quais-as-diferentes%C3%A7as-e omo-usar

1 answer

3

TL;DR

It depends on the point of view, but in general aggregation in a class model occurs only when there is an attribute.

Class structure

In general, when we talk about class diagrams, we are referring to structure of them. Therefore, aggregation would only occur in the case of A have an attribute of the type B.

We are not concerned with implementation details while modeling or doing a high-level system design.

Static analysis of the code

On the other hand, if we make a static code analysis of a class ready to determine dependencies, we can find them:

  • Explicit or direct: in variable types and parameters, for example. Usually there will be a import for each reference.
  • Implicit or indirect: when using an interface without knowing the implementation or when there is call chaining, such as in objetoB.getObjetoC().getAtributo(), because then we depend on C without a direct link to the A.

Dynamic analysis in progress

Going deeper, we can still make a run-time analysis to determine the actual dependencies a class needs to run.

It is considered here that some of the interface implementations can be provided:

  • At runtime, for example through plugins or by libraries that are not present during compilation
  • By using reflection, instantiating or accessing classes by their name
  • According to user input or program configuration, where instance types may vary
  • For libraries that generate classes dynamically

Considerations

Structural class modeling is good for thinking about the system domain, but it does not guarantee or attempt to guarantee the way the system performs nor its dependencies at runtime.

Static code analysis helps determine the coupling and cohesion of a class, so one should always try to decrease the amount of dependencies.

Finally, runtime analysis is the only one that gives full assurance of what is actually being performed, but it is also the most costly and complex. Still, it is time-dependent, which means that the behavior of the system varies over time of use and sometimes it is very difficult to reproduce the desired scenario.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.