These concepts describe relationships between objects in a class hierarchy.
We’ll break them down one by one below.
Association
Association represents a “uses-a” or “has-a” relationship between two separate classes where one class uses the other. It defines a relationship between objects where one object can access another.
For example, in C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class Driver
{
public string Name { get; set; }
}
public class Car
{
public string Model { get; set; }
public Driver Driver { get; set; } // Association with Driver class
}
public class Program
{
public static void Main()
{
Driver driver = new Driver { Name = "John" };
Car car = new Car { Model = "Toyota", Driver = driver };
Console.WriteLine($"{car.Driver.Name} drives a {car.Model}");
}
}
|
Aggregation
Aggregation is a specialized form of Association with a “whole-part” relationship, but the lifetimes of the parts are independent of the whole. In other words, the part can exist without the whole.
For example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
public class Department
{
public string Name { get; set; }
}
public class Company
{
public string Name { get; set; }
public List<Department> Departments { get; set; } = new List<Department>(); // Aggregation
public void AddDepartment(Department department)
{
Departments.Add(department);
}
}
public class Program
{
public static void Main()
{
Department d1 = new Department { Name = "HR" };
Department d2 = new Department { Name = "Finance" };
Company company = new Company { Name = "TechCorp" };
company.AddDepartment(d1);
company.AddDepartment(d2);
Console.WriteLine($"{company.Name} has the following departments:");
foreach (var dept in company.Departments)
{
Console.WriteLine(dept.Name);
}
}
}
|
Composition
Composition is a stronger form of Aggregation with a “part-whole” relationship where the part can’t exist without the whole. If the whole is destroyed, the parts are also destroyed.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
public class Engine
{
public string Model { get; set; }
public Engine(string model)
{
Model = model;
}
}
public class Car
{
public string Model { get; set; }
public Engine Engine { get; set; } // Composition
public Car(string model, string engineModel)
{
Model = model;
Engine = new Engine(engineModel);
}
}
public class Program
{
public static void Main()
{
Car car = new Car("Toyota", "V8 Engine");
Console.WriteLine($"Car model: {car.Model}, Engine model: {car.Engine.Model}");
}
}
|
Summary
- Association represents a general relationship where one class uses another. There is no ownership implied. In the example: a
Carhas a Driver.
- Aggregation defines a specialized form of Association with a “whole-part” relationship where the part can exist independently of the whole. In the example, a
Companyhas Departments, but Departmentscan exist without the Company.
- Composition describes a strong form of Aggregation where the part cannot exist independently of the whole. If the whole is destroyed, the parts are also destroyed. In the example, a
Carhas an Engineand that Enginecannot exist independently of the Car.
Photo by picjumbo.com.