473,394 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

composition and aggregation ???

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...
Nov 16 '05 #1
4 10942
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" <cm****@yahoo.com> wrote in message
news:em*************@TK2MSFTNGP09.phx.gbl...
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...

Nov 16 '05 #2
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...
Nov 16 '05 #3
"Chris C" <cm****@yahoo.com> a écrit dans le message de news:
em*************@TK2MSFTNGP09.phx.gbl...
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
Nov 16 '05 #4
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.

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: DKode | last post by:
I think i have a good handle on aggregation/composition. can someone tell me if i am headed in the right direction with my analysis below: We have the following objects: Employee...
1
by: Chris K | last post by:
I am relatively new to C++ and hope that this question is relevant. I have spent some time at the local library and some time on dejanews, but have no decided to go ahead with my question, since...
1
by: Nice Chap | last post by:
Aggregation in COM was defined as 'Exposing an interface of an inner object by the outer object as though it were an interface of the outer object'. Is this type of aggregation possible in c#? ...
4
by: Frederik Vanderhaegen | last post by:
Hi, Can anyone explain me the difference between aggregation and composition? I know that they both are "whole-part" relationships and that composition parts are destroyed when the composition...
2
by: Gary Wessle | last post by:
Hi what is the difference "in code writing" between aggregation and composition? is the following correct? aggregation (life time of both objects are independent). class A{ /* ... */ };...
4
by: fireball | last post by:
hi, I got confused for a moment about creating data structure for UML composition (strong aggregation) relation one-to-many. I used Rose/DataModeler to do so. <filled_diamond>-------- I...
4
by: Salman | last post by:
The most confusing concept i have seen in OOP is the difference between Association, Composition and Aggregation. Anyone please tell me whats the actual difference between these three using...
7
by: dev_15 | last post by:
Hi, just a simple question in an OO design in C++ if i am using composition to model a relationship i would have the object as a member(or pointer member) of the class ie class Airport {...
7
by: snewman18 | last post by:
In learning about design patterns, I've seen discussion about using inheritance when an object's relationship to another object is 'is-a' and composition when the relationship is 'has-a'. Since...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.