473,748 Members | 8,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Aggregation - Composition

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 whole is destroyed.
Under a "whole-part" relationship I understand the following:
the whole can't exists without the parts, but can the parts exist without
the hole?
f.e.: a car can't exist without an engine
private engine _Engine
public car(engine oEngine){
this._Engine=oE ngine
}

but, is it possible to create an engine without that a car has been
created?

Is it possible to provide me a code sample because I don't have a clue how
to implement this and a sample says more than a 1000 words.

Please correct me if I'm wrong.

Thanks in advance

Frederik
Nov 17 '05 #1
4 14448

"Frederik Vanderhaegen" wrote...
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 whole is destroyed.
Under a "whole-part" relationship I understand the following:
the whole can't exists without the parts, but can the parts exist without
the hole?
f.e.: a car can't exist without an engine
private engine _Engine
public car(engine oEngine){
this._Engine=oE ngine
}

but, is it possible to create an engine without that a car
has been created?


That's both the point and the question.

Both association and composition as OO concepts are just that, *concepts* to
be used at the "conceptual level" when you're designing your application.

The "car-engine" example as such isn't that good as an example, as you can
decide to go either way, or rather, it depends on what you want to allow.

It also seems that you're somewhat confused to what these concepts actually
means.

Composition: When you "destroy" the whole, the parts are also destroyed.
Aggregation: When you "destroy" the whole, the parts can still continue to
exist.

A pretty clear (and classic) example of "aggregatio n" is a course with
students. You can say that the course "aggregates " its students, but when
you delete the course from the system (e.g. it's finished), the students
themselves remain, e.g. as students in other courses.

It's harder to find the need for "true" composition, as you in almost any
case can find some possiblity to "reuse" the parts. But that's why they're
just "concepts". If you don't find a need to keep the engine when the car
crashed, that's your choice... ;-)

An example I sometimes use is a hotel with rooms. The hotel is composed of
rooms, as they won't exist without the hotel.

Implementation-wise...
They're only references anyway... ;-)

HTH

// Bjorn A

Nov 17 '05 #2
Containment by ownership:
class Car : Vehicle
{
Radio r= new Radio();
}

An instance of Car contains an instance of a Radio so that the lifetimes
of the radio and car are intertwined. This is "containmen t by ownership"
making Car an example of a composite class.

Containment can also be accomplished using references to external
objects. "Containmen t by reference" can be expressed in code thusly:

class Radio
{
...
}
class Vehicle
{
...
}
class Car : Vehicle
{
private Radio r;
public Car(Radio r) {
...
this.r= r;
}
}
You can then create an instance of Car like this:

class Class1
{
[STAThread]
static void Main(string[] args)
{
Radio r= new Radio();
Car c= new Car(r);
}
}

A real world example would be a custom collection. Your custom class
could contain the actual collection by ownership and provide type safe,
null safe setters and getters.

If you then want to pass a read only version of the custom class, you
would create a new class that wraps the read write collection and
provides only getters, a so called immutable class. You would _not_ need
to copy the collection, but could simply pass a reference to the
existing custom class. The read only class would use containment by
reference and avoid the overhead of a deep copy of the collection.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
Now I understand the difference between aggregation and composition but what
do you mean *concepts*?
Is there no way to implement aggregation and composition? Do they behave
like an association?
As you can read I'm very new in OO, so any help is welcome :$

thx.

"Bjorn Abelli" <bj**********@D oNotSpam.hotmai l.com> schreef in bericht
news:OV******** ******@TK2MSFTN GP09.phx.gbl...

"Frederik Vanderhaegen" wrote...
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 whole is destroyed.
Under a "whole-part" relationship I understand the following:
the whole can't exists without the parts, but can the parts exist
without the hole?
f.e.: a car can't exist without an engine
private engine _Engine
public car(engine oEngine){
this._Engine=oE ngine
}

but, is it possible to create an engine without that a car
has been created?


That's both the point and the question.

Both association and composition as OO concepts are just that, *concepts*
to be used at the "conceptual level" when you're designing your
application.

The "car-engine" example as such isn't that good as an example, as you can
decide to go either way, or rather, it depends on what you want to allow.

It also seems that you're somewhat confused to what these concepts
actually means.

Composition: When you "destroy" the whole, the parts are also destroyed.
Aggregation: When you "destroy" the whole, the parts can still continue to
exist.

A pretty clear (and classic) example of "aggregatio n" is a course with
students. You can say that the course "aggregates " its students, but when
you delete the course from the system (e.g. it's finished), the students
themselves remain, e.g. as students in other courses.

It's harder to find the need for "true" composition, as you in almost any
case can find some possiblity to "reuse" the parts. But that's why they're
just "concepts". If you don't find a need to keep the engine when the car
crashed, that's your choice... ;-)

An example I sometimes use is a hotel with rooms. The hotel is composed of
rooms, as they won't exist without the hotel.

Implementation-wise...
They're only references anyway... ;-)

HTH

// Bjorn A

Nov 17 '05 #4
"Frederik Vanderhaegen" wrote...
Now I understand the difference between aggregation and
composition but what do you mean *concepts*?
With "concepts" I mean that it's something you use at the conceptual level
to try to understand how different object will relate to each other, i.e. in
the design phase before you start to code.
Is there no way to implement aggregation and composition?
There's of course ways to implement aggregation and composition differently,
but in most cases simple aggregation will suffice for the patterns where you
conceptually want a composition.

Let's look at an example:

=============== =============== =============== =

class Hotel
{
private ArrayList rooms = new ArrayList();

public void CreateRoom()
{
rooms.Add(new Room());
}

private class Room { }
}

=============== =============== =============== =

The construction above could be seen as a strict
kind of composition, but to encapsulate the class
Room in this way can be impractical.

=============== =============== =============== =
public class Hotel
{
private ArrayList rooms = new ArrayList();

public void AddRoom(Room r)
{
rooms.Add(r);
}
}

public class Room {}

=============== =============== =============== =
The latter example can *still* be used for
composition, as long as the Rooms you instantiate
*only* will be referenced from the Hotel's
ArrayList of rooms.

The main difference is that you've opened up
for situations where Rooms *can* be referenced
from other places as well.

If you *conceptually* want a composition, but the
latter construct, just make sure the Rooms only
will be used within the Hotels.

=============== =============== =============== =
Do they behave like an association?
As you can read I'm very new in OO, so any help is welcome :$


In both cases they're "specialize d concepts of associations".. . ;-)

As I said before:
Implementation-wise...
They're only references anyway... ;-)


// Bjorn A
Nov 17 '05 #5

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

Similar topics

0
1918
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 EmployeeCollection Hour HourCollection Infraction
1
2492
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 I found no satisfactory answer yet. It is about composed/aggregated classes. I am developing a scientific code (Monte Carlo) in which I find it natural to have classes with several levels of aggregation.
4
10967
by: cmrchs | last post by:
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
11
370
by: aaj | last post by:
Hi is it possible in VB.NET to aggregate two classes, if so could you tell me how its done? I have been looking for a while and can't find any examples that actually show the physical code. Perhaps I'm using the wrong terminology? thanks in advance
23
2125
by: SenthilVel | last post by:
Hi Can any one let me know the websites/Pdf for learning Aggragation in C#?? Thanks Senthil
7
15196
by: Bruce One | last post by:
In C#, how would people implement a relationship between Customer class and Request class, considering a customer may have 0-n requests and a request must belong to 1 and only 1 customer...? Would about the following options? OPTION 1 -------------------------- public class Customer() { private int CustomerID;
2
14826
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{ /* ... */ }; class B{ A& a; /* ... */};
6
7848
by: Jeff | last post by:
hey Can OO Aggregation be described as: - A system of objects that are built using each other any comments? Jeff
4
17163
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 EXAMPLES in c+ +. I shall be very very thankful to you if you prove these concepts using examples.
0
9534
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9316
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9241
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.