Aggregation

  • Thread starter Thread starter aaj
  • Start date Start date
A

aaj

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

Andy
 
aaj,

I don't know if it is the wrong terminology, however some weeks ago I saw a
very long discussiong because somone had another meaning of the word
aggregate and they started a discussion about that.

Therefore what do you mean with aggregate by this.

Cor
 
Hi Cor

Say I have class A ,class B and class C all three are unique and different.

for instance a wheel, a pedal and a handlebar

I DONT want Class C to inherit from class B which inherits from Class A.
i.e. I dont want an 'IS A' type relationship

what I want to make is a new class D that contains selected methods from
class A, selected methods from class B and selected methods from class C
i.e. they are 'parallel' with each other. i.e. a 'Has a' type relationship

The thing I'm trying to achieve is shown by either an open or filled diamond
shape in uml 2.0

Thanks

Andy
 
Herfried,

Thanks, you should have read a long thread I thought in the general
newsgroup where 2 persons were almost killing each other because of that one
of those had used aggregate and the other had onother meaning of aggregate.
The one who used it is let us say "more theoretic than me."

:-)

Cor
 
aaj,

You mean that your car has a property wheel, a property pedal and a propety
handlebar.

The value of those are a wheelobject a pedalobject and a handlebar object.
Because it is a class, it are not collections?

I don't think that a car has methods from a wheel, a pedal or a handlebar
that are carparts.

Thererfore you can have a class carparts.

And a class wheel, pedal and handlebar, which inherits than from carparts.

Is that what you mean?

Cor
 
Similar but not exactly

I want to have a wheel that is a base class, a pedal thats a base class and
a handlebar thats a base class

I want a new object called a bike that is an aggregate of a wheel, a
handlebar and a pedal

i.e. my bike 'has a' handlebar and 'has a' wheel (its a unicycle) and 'has
a' pedal (only one at this stage)


I hope I'm asking the right question here, but I'm pretty sure I'm not
looking for inheritance

hope it makes sense

Andy
 
Hi aaj,

I would suggest defining interfaces for IWheel, IPedal, and IHandlebar, then
having Class A, B or C implementing those interfaces where appropriate. If
the implementation is common (ie IWheel.Rotate would be implemented the same
in Class A, B or C) then I would have an internal Friend class which
provides the common code for this that you can just call into, therefore
preventing duplication of code.

I believe what you really want to accomplish would be possible if .NET
supported multiple object inheritance, which it doesn't (however I believe
C++ does?). That way, Class A could inherit from Wheel and Pedal, Class B
could inherit from Pedal and Handlebar etc. I've always this would be a
nice feature in .NET, but I can understand why MS chose not to implement it
as part of the CLR.

Cheers,
Alex Clark
 
aaj said:
Similar but not exactly

I want to have a wheel that is a base class, a pedal thats a base class and
a handlebar thats a base class

I want a new object called a bike that is an aggregate of a wheel, a
handlebar and a pedal

i.e. my bike 'has a' handlebar and 'has a' wheel (its a unicycle) and 'has
a' pedal (only one at this stage)

Public Class Bike
Public bikehandle As Handlebar
Public bikewheel As Wheel
Public bikepedal As Pedal
End Class

(implement with formal properties rather than public members if you
prefer)

Is this good enough?
 
aaj,

Change the word car completly for bicycle in my message.

I gave exactly the answer.

A wheel can be a base class for the front wheel and the backwheel because
they are on a bike completly different . However a pedal can never be a
baseclass in this situation.

A bike has pedals, a pedal has a cone.
However a pedal has not a bike, not a wheel and as well not a handle.

It is possible to inherit a bike from a class bicycle part, however I would
not do that because it is the top of the three and has only properties that
aggregate as you saw..

You get than what Larry wrote and than as well something as

Class bicyclepart
Public property colour

Class wheel
inherits bicyclepart
Public property spokes as spoke "an object that as well inherits from
bicyclepart
etc

Class pedal
inherits bicyclepart
public side as enumside

Class handle
inherits bicyclepart
public side as enumside


Cor
 
Very interesting link Herfried. Thank you. I am learning OOP and in my
notes had written about the "has a" relationship but I did not
consider the difference between aggregation and composition.

As I understand it, if it is aggregation then the individual objects
are properties of the base object and are thus destroyed when the base
object is destroyed.

But in composition, only a reference to the individual objects are in
the base object so when the base object is destroyed, the individual
objects themselves still persist.

If that is true, would one implement aggregation with properties that
are the individual objects and implement composition with properties
that are ArrayLists containing the individual objects?

TIA,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top