Aggregation vs Composition vs Association vs Direc

2019-01-16 01:31发布

I am reviewing my knowledge in object-oriented programming. Under the relationship between classes topic, I have encountered some relationships which are a bit ambiguous to me.

I know dependency "uses-a" and inheritance "is-a" but I'm a bit unfamiliar with Aggregation, Composition, Association and Direct Association; also, which of them is "has-a" relationship. Some use Aggregation interchangeably with Association.

What is Direct Association? Also, what is Composition? In UML diagrams, the arrows that represents them are different. I would be really thankful if you could clear these things out for me.

4条回答
祖国的老花朵
2楼-- · 2019-01-16 02:09

An association between object types classifies relationships between objects of those types. For instance, the association Person-isEmployedBy-Enterprise may classify the relationships PeterMiller-isEmployedBy-IBM, SusanSmith-isEmployedBy-IBM and SarahAnderson-isEmployedBy-Google between the objects PeterMiller, SusanSmith and SarahAnderson of type Person as well as Google and IBM of type Enterprise. In other words, associations are relationship types with two or more object types participating in them. An association between two object types is called binary. While binary associations are more common, we may also have to deal with n-ary associations, where n is a natural number greater than 2. For instance, Person-isTreatedIn-Hospital-for-Disease is a 3-ary ("ternary") association between the object types Person, Hospital and Disease.

I guess that with "direct association" you mean a directional (or directed) association, which is an association (with a domain class and a range class) that represents a reference property in its domain class. Such a directional association has an "ownership dot" at its target end.

Please see this book chapter for more about associations.

And see my answer to this SO question for an explanation of aggregations and compositions.

查看更多
ら.Afraid
3楼-- · 2019-01-16 02:15

Direct association has nothing in common with the other three. It does not belong to UML at all, it is the IBM requirements modelling term.

As for others,

Association A->B is a child of Dependency. Association means, that A (or its instance) has some easy way to get to instance of B. For example, a.x.y.b. Or by function, or by some local variable. Or by a direct reference or pointer, or something else (there are many languages in the world). As you see, there is no strict border between dependency and association.

One of attributes of Association is Aggregation, it can have values: None, shared (often incorrectly called aggregation), and composition.

If A (or instance) has some (or one) instances of B so, that destroying of association means the destroying of B instances, it is the composition.

If you or a tool author had decided, that some has-a relationship, that is weaker that composition, needs to be specially shown, you can use shared aggregation. Usually it is some collections of references to B in A.

There are some more interesting attributes of associations. Look here if you are interested.

查看更多
再贱就再见
4楼-- · 2019-01-16 02:25

For example The whole point of OOP is that your code replicates real world objects, Therefor making your code readable and maintainable.

  1. Association

Association basically is Class A uses Class B. As an example

  • Employee uses Bus/train Services for transportation.
  • Computer uses keyboard as input device

And in In UML diagram Association is denoted by normal arrow head.

  1. Aggregation Class A contains Class B , Or Class A has instance of Class B. An aggregation is used when life of object is independent of container object But still container object owns the aggregated object.

So if we delete class A that doesn't mean that class B will also be deleted , as an example (non or many teachers can belong to one or many departments) the relationship between Teachers and departments is aggregation.

  1. Composition Class A owns Class B. As an example , Body consists of Arm, Head, Legs , BankAccount consists of Balance and TransactionHistory. So if class be gets deleted then also class B will get deleted.
查看更多
Juvenile、少年°
5楼-- · 2019-01-16 02:28

Please note that there are different interpretations of the "association" definitions. My views below are heavily based on what you would read in Oracle Certification books and study guides.

Temporary association

A usage inside a method, its signature or as a return value. It's not really a reference to a specific object.

Example: I park my Car in a Garage.

Temporary Association UML

Composition association

A so-called "STRONG relationship": The instantiation of the linked object is often hard coded inside the constructor of the object. It cannot be set from outside the object. (Composition cannot be a many-to-many relationship.)

Example: A House is composed of Stones.

Composition UML

Direct association

This is a "WEAK relationships". The objects can live independent and there are usually setters or other ways to inject the dependent objects.

Example: A Car can have Passengers.

Direct Association UML

Aggregation association

Very similar to a Direct association. It's also a "WEAK relationship" with independent objects. However here the associated objects are a crucial part of the containing object.

Example: A Car should have Tires.

Aggregation UML

Note: Both Direct associations and Aggregation associations are often generalized as "Associations". The difference is rather subtle.

查看更多
登录 后发表回答