"Chris C" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...
> how do I implement aggregation and how composition in C# ?
> When I say : an Airplane has a Pilot then I use aggregation but
> when I say : an Airplane has a Cockpit then I use composition.
> How do I implement the difference in C# ?
Aggregation is when a containing object holds references to other objects,
but those other objects have a life of their own.
Composition is when other objects are owned by a containing object and their
lifetime is managed by the containing object.
I can't make your example work as both a Pilot and a Cockpit can be moved
from one plane to another; ergo they are both aggregations.
The essential difference is the lifetime management of child objects
// Aggregation
class StockLocation
{
...
public void AddProduct(Product p) {...}
}
You can create Product instances outside of the StockLocation and you can
move them from one location to another.
// Composition
class Invoice
{
...
public InvoiceLine AddLine() {...}
}
You have to ask the Invoice to create an instance of an Invoice Line.
Does this help ?
Joanna
--
Joanna Carter
Consultant Software Engineer