Express aggregation in C#
To model the relationship between customers and addresses, it would make sense to say a Customer 'has-a' Address instead of saying an Address is 'part-of' the Customer. Aggregation is shown on a UML diagram as an unfilled diamond.
So how do we express the concept of aggregation in C#? Well, it's a little different from the composition. Consider the following code:
public class Address
{
. . .
}
public class Person
{
private Address address;
public Person(Address address)
{
this.address = address;
}
. . .
}
A person would then be used as follows:
Address address = new Address();
Person person = new Person(address);
or
Person person = new Person( new Address() );
As you can see, a Person does not manage the lifetime of an Address. If the Person is destroyed, the Address still exists. This scenario does map quite nicely to the real world.