When do you prefer to use composition than aggregation?
By in Design Patterns & Practices on Jan 11 2007
  • Shanmugam Raman Palaniappan
    May, 2008 21

    Composition - Composition that is used to store several instances of the composited data type is referred to as containment. Examples of such containers are arrays, linked lists, binary trees and associative arrays.

    Aggregation - This implies a "has a" relationship, is a logical relationship. One class is constructed from other classes, that is, it contains objects of any component classes. For example, a car class would contain objects such as tires, doors, engine, and seats.

    [Aggregation differs from ordinary composition in that it does not imply ownership. In composition, when the owning object is destroyed, so are the contained objects. In aggregation, this is not necessarily true. For example, a university owns various departments (e.g., chemistry), and each department has a number of professors. If the university closes, the departments will no longer exist, but the professors in those departments will continue to exist. Therefore, a University can be seen as a composition of departments, whereas departments have an aggregation of professors. In addition, a Professor could work in more than one department, but a department could not be part of more than one university.]

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS