UML: Dependency, Association, Aggregation, Composition, Generalization, Realization
This article explains the six fundamental UML relationship types—Dependency, Association, Aggregation, Composition, Generalization, and Realization—detailing their semantics, visual notations, code examples in Java/C++, and the distinctions among them, helping developers model object interactions accurately.
UML describes various ways objects and classes can relate, including Dependency, Association, Aggregation, Composition, Generalization, and Realization.
Dependency
Dependency means element A changes affect element B, but not vice‑versa; B depends on A. It is shown with a dashed arrow pointing to the depended‑on element.
Characteristics: one‑way, often appears as local variables, method parameters, or static method calls in Java/C++.
public class Person {
void buy(Car car) {
...
}
}Association
Association is a structural, usually bidirectional, relationship where one class knows another class's attributes and methods. In code it is implemented via member variables.
public class Disciple {
// ...
}
public class TangSeng {
protected: list<Disciple> tdlist;
}Aggregation
Aggregation is a special form of association that represents a whole‑part relationship (has‑a). The part can exist independently of the whole.
public class Engine {
// ...
}
public class Tire {
// ...
}
public class Car {
protected: Engine engine;
protected: Tire tyre[4];
}Composition
Composition is a stronger form of aggregation where the part’s lifecycle is bound to the whole; when the whole is destroyed, its parts are destroyed as well.
class Limb {
// ...
}
class Person {
protected: Limb limb[4];
}Generalization
Generalization models inheritance (is‑a) relationships between a more general element and a more specific one. In Java it is expressed with the extends keyword.
Realization
Realization models the contract between an interface (the definition) and a class that implements it. In Java it is expressed with the implements keyword.
Differences Between Relationships
Aggregation vs. Composition : Both are whole‑part relationships, but in aggregation the part can exist independently of the whole, while in composition the part’s lifecycle is tied to the whole.
Association vs. Aggregation : Association is a generic structural link; aggregation adds the whole‑part semantic.
Association vs. Dependency : Association represents a stronger, often long‑term structural link, whereas dependency is a weak, temporary usage relationship.
Generalization vs. Realization : Generalization models inheritance between classes (or interfaces), while realization models a class fulfilling an interface contract.
Overall, the strength of these relationships can be ordered as: Composition > Aggregation > Association > Dependency.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
