The concepts of composition and aggregation are displayed with arrows with diamonds in UML, but when those concepts are implemented in a programming language, may change from one programing language to another.
Generally speaking, in programming languages, like Java, C# or Delphi, both cases are represented by object references. Each object has a "life cycle" ("created", "do stuff", "destroyed").
package Universities;
class ProfessorClass {
string FirstName;
string LastName;
// constructor
public ProfessorClass(string AFirstName, string ALastName) {
FirstName = AFirstName;
LastName = ALastName;
}
} // class ProfessorClass
class DepartmentClass {
string Name;
string Description;
// constructor
public DepartmentClass(string AName, string ADescription) {
Name = AName;
Description = ADescription;
}
} // class DepartmentClass
class UniversityClass {
// here doesn't look different:
// aggregation
ProfessorClass[] Professors;
// composition
DepartmentsClass[] Departments;
// constructor
public UniversityClass() {
// "UniversityClass" is in charge of creating parts
// here doesn't look different:
DepartmentsClass = new DepartmentsClass[]();
ProfessorClass = new ProfessorClass[]();
}
public addDepartment(string AName, string ADescription)
{
// composition, whole class is in charge of adding parts:
DepartmentClass Dept = new DepartmentClass(AName, ADescription);
DepartmentsClass.add(Dept);
}
public deleteDepartment(string AName)
{
// composition, whole class is in charge of deleting parts:
DepartmentsClass.delete(AName);
}
public addProfessor(ProfessorClass AProfessor)
{
// aggregation, whole class only reference other objects,
// but, may look like they where parts
ProfessorClass.add(AProfessor);
}
public static void main(String[] args) {
UniversityClass MyUniversity = new UniversityClass();
// composition, internal, maintained by main class
MyUniversity.addDepartment("History", "Very Boring");
MyUniversity.addDepartment("Music", "Only if you like it");
MyUniversity.addDepartment("Astronomy", "night living people");
// aggregation, external, referenced by main class,
// maintained independently
ProfessorClass Professor1 = new ProfessorClass("Jane", "Doe");
ProfessorClass Professor2 = new ProfessorClass("Mike", "Smith");
ProfessorClass Professor3 = new ProfessorClass("Louise", "Kent");
MyUniversity.addProfessor(Professor1);
MyUniversity.addProfessor(Professor2);
MyUniversity.addProfessor(Professor3);
} // static void main(...)
} // class UniversityClass
In composition, "whole" objects that are composed by other objects ("parts"), are in charge of creating, using & destroying its parts. The "whole" object its responsible for the "life cycle" of each of its "parts".
In aggregation, "whole" objects that reference other objects ("aggregation"), that may look similar to "parts" (composition), usually, are not created or destroyed by the main object, just assigned or deassigned. The main object doesn't control directly the other objects, just add or remove references.
There are some times you may pick code alredy done from other developers, and may see both ideas, mixup, and cannot difference which concept is been used.