PC Review


Reply
Thread Tools Rate Thread

composition and aggregation ???

 
 
cmrchs@yahoo.com
Guest
Posts: n/a
 
      16th Feb 2005
Hi,

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# ?

Here's what I try :

class Pilot
{....}

class Cockpit
{...}

class Airplane
{
Pilot p;
Cockpit c;

public Airplane()
{
p = new Pilot(); // must be aggregated
c = new Cockpit(); // must be composed
}
}

I don't see any difference. Is there a way to create the distinction ?

thanks
Chris

**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
 
Reply With Quote
 
 
 
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      16th Feb 2005
Hi,

There is no way of doing it, these are logic interpretation concepts that
are expressed in tha same way.

Why you want to express them in different ways?

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"Chris C" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> 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# ?
>
> Here's what I try :
>
> class Pilot
> {....}
>
> class Cockpit
> {...}
>
> class Airplane
> {
> Pilot p;
> Cockpit c;
>
> public Airplane()
> {
> p = new Pilot(); // must be aggregated
> c = new Cockpit(); // must be composed
> }
> }
>
> I don't see any difference. Is there a way to create the distinction ?
>
> thanks
> Chris
>
> **********************************************************************
> Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
> Comprehensive, categorised, searchable collection of links to ASP &
> ASP.NET resources...



 
Reply With Quote
 
cmrchs@yahoo.com
Guest
Posts: n/a
 
      16th Feb 2005
hi Ignacio,

cause in C++ you can do it by :

class Airplane
{
Pilot *p; // aggregation
Cockpit c; // embedded object, thus composition

public Airplane()
{
p = new Pilot();
}
}

but you say it doesn't matter then ?
for the pilot OK since in C# reference variables are used anyway, but I was wondering for the cockpit.
In C# is the cockpit not really 'built-in' in the Airplane-object anymore since a reference is used then as well.
How can I make sure then that lifetime of the supposed embedded Cockpit will not extend that of the Airplane ?

thanks
Chris

**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
 
Reply With Quote
 
Joanna Carter \(TeamB\)
Guest
Posts: n/a
 
      16th Feb 2005
"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


 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      16th Feb 2005
Correct me if I'm wrong, but part of the difference here (in C#, at
least) has to do with the functionality that the enclosing object
offers to its callers.

To go back to the airplane and cockpit example (composition, the
problematic case), you want the airplane to create its own cockpit
(possibly using information passed on the constructor), and then the
Cockpit method of the Airplane has no setter:

public Cockpit Cockpit
{
get { return this.cockpitComponent; }
}

....because, if it offered a setter, then you could get the cockpit from
one plane and assign it to another, breaking the rules of composition.

The fact that C# actually stores the cockpit as a separate heap object,
and that airplane has only a reference to it, doesn't matter so much as
how you design your airplane object to ensure that callers can't assign
cockpits to airplanes by any means.

If you structure the enclosing object in this way--creates its own
child object, and won't let any caller assign a new child object--then
what you have, in effect, is composition. If you allow callers to
assign a child object, then what you have, in effect, is aggregation.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Aggregation... =?Utf-8?B?VGVkIEJ1b3k=?= Microsoft Excel Misc 2 11th Jun 2007 08:15 PM
Aggregation - Composition Frederik Vanderhaegen Microsoft C# .NET 4 3rd Nov 2005 12:50 PM
aggregation in C# Jozsef Bekes Microsoft C# .NET 0 26th May 2005 03:43 PM
Association/Aggregation/Composition DKode Microsoft Dot NET 0 7th Nov 2003 02:51 PM
COM Aggregation in VB.NET Mike Hamsa Microsoft VB .NET 2 22nd Oct 2003 02:41 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:28 PM.