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.
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 typePerson
as well as Google and IBM of typeEnterprise
. 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 typesPerson
,Hospital
andDisease
.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.
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.
For example The whole point of OOP is that your code replicates real world objects, Therefor making your code readable and maintainable.
Association basically is Class A uses Class B. As an example
And in In UML diagram Association is denoted by normal arrow head.
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.
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
Example: I park my Car in a Garage.
Composition association
Example: A House is composed of Stones.
Direct association
Example: A Car can have Passengers.
Aggregation association
Example: A Car should have Tires.
Note: Both Direct associations and Aggregation associations are often generalized as "Associations". The difference is rather subtle.