Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregation vs Composition vs Association vs Direct Association

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.

like image 516
steven0529 Avatar asked Feb 23 '14 11:02

steven0529


People also ask

What is the difference between association and direct association?

If you use association end names, you do not typically need to name an association. You can, however, name any association to describe the nature of the relationship between the two classifiers. A directed association is displayed as a solid line with an arrow that indicates the direction of navigation.

What is directed association in UML?

The directed association is concerned with the direction of flow inside association classes. The flow of association can be shown by employing a directed association. The directed association between two classes is represented by a line with an arrowhead, which indicates the navigation direction.

What is the composition association?

A composition association relationship represents a whole–part relationship and is a form of aggregation. A composition association relationship specifies that the lifetime of the part classifier is dependent on the lifetime of the whole classifier.

What are the different types of association in UML?

An association can be categorized into four types of associations, i.e., bi-directional, unidirectional, aggregation (composition aggregation), and reflexive, such that an aggregation is a special form of association and composition is a special form of aggregation.


2 Answers

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.

like image 67
bvdb Avatar answered Oct 22 '22 09:10

bvdb


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

1. Association

Association is: Class A uses Class B.

Example:

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

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

2. Aggregation

Class A contains Class B, or Class A has an 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. E.g. none, or many, teachers can belong to one or many departments.

The relationship between Teachers and Departments is aggregation.

3. Composition

Class A owns Class B.

E.g. Body consists of Arm, Head, Legs. BankAccount consists of Balance and TransactionHistory.

So if class A gets deleted then also class B will get deleted.

like image 16
user 451 Avatar answered Oct 22 '22 10:10

user 451