What is the difference between aggregation, composition and dependency?

sevugarajan picture sevugarajan · Oct 29, 2009 · Viewed 281.7k times · Source

What is the difference between aggregation, composition and dependency?

Answer

cletus picture cletus · Oct 29, 2009

Aggregation implies a relationship where the child can exist independently of the parent. Example: Class (parent) and Student (child). Delete the Class and the Students still exist.

Composition implies a relationship where the child cannot exist independent of the parent. Example: House (parent) and Room (child). Rooms don't exist separate to a House.

The above two are forms of containment (hence the parent-child relationships).

Dependency is a weaker form of relationship and in code terms indicates that a class uses another by parameter or return type.

Dependency is a form of association.