C# code for association, aggregation, composition
Asked Answered
I

4

47

I am trying to confirm my understanding of what the code would look like for association, aggregation & composition. So here goes.

Aggregation: Has-a. It has an existing object of another type

public class Aggregation
{
    SomeUtilityClass objSC
    public void doSomething(SomeUtilityClass obj)
    {
      objSC = obj;
    }
}

Composition: Is composed of another object

public class Composition
{
    SomeUtilityClass objSC = new SomeUtilityClass();
    public void doSomething()
    {
        objSC.someMethod();
    }
}

Association: I have two views on this.

  1. When one class is associated with another. Hence both the above are examples of association.

  2. Association is a weaker form of Aggregation where the class doesn't keep a reference to the object it receives.

    public class Association
    {
        //SomeUtilityClass objSC   /*NO local reference maintained */
        public void doSomething(SomeUtilityClass obj)
        {
           obj.DoSomething();
        }
    }
    

Is my understanding correct? I have read conflicting articles here and here and so I am really not sure which one to follow. My understanding seems to be in line with the first link. I feel the second link is wrong, or maybe perhaps I haven't understood it properly.

What do you think?

Interdenominational answered 26/9, 2012 at 14:29 Comment(4)
aggregation and association are not commonly used terms in my experience. I wouldn't fuss too much about knowing the subtleties of their definition. There are much more interesting concepts for you to spend your time on.Vignola
thanks Servy; but is my understanding described above correct?Interdenominational
c-sharpcorner.com/UploadFile/pcurnow/compagg07272007062838AM/…Poker
@Vignola I know this is an old article but I have been struggling with same issue for sometimes as I am learning on my own. I was wondering, based on your comment above, what concepts would you suggest to learn?Lymphangitis
A
26

The difference between aggregation and composition is pretty fuzzy and AFAIK relates to the logical existence of the "child" objects after the container is destroyed. Hence, in the case of aggregation the objects inside the container can still exist after the container object is destroyed, while in the case of composition design demands that they also get destroyed. Some analogies:

  • A Car object containing four Wheel objects. Normally if you destroy the car (by calling some method to clean it up) you should also destroy the wheels in the process, since it makes little sense for them to exist outside the car (unless you move them to another Car object). More realistically, a reader object wrapping an input stream will also close the inner stream when it gets closed itself. This is composition.
  • A Person object contains (owns) a Radio object. If the Person dies, the Radio may be inherited by another Person i.e. it makes sense for it to exist without the original owner. More realistically, a list holding objects does not demand that all objects get disposed when the list itself is disposed. This is aggregation.

Edit: After reading your links I'd be inclined to go with the first one, since it gives an explanation similar to mine.

Also association is merely invoking a method (sending a message) to another object via a reference to that object.

Alitaalitha answered 26/9, 2012 at 14:40 Comment(2)
@user20358: Association is generally defined as calling a method on another object. It doesn't matter if you keep a reference of not. Technically you can have aggregation without association: just hold some objects but don't use them for anything. :DAlitaalitha
Good examples...very helpfull.Ionia
H
69

For two objects Foo and Bar we have:

Dependency:

class Foo
{
    ...
    fooMethod(Bar bar){}
    ...
}

Association:

class Foo
{
    private Bar bar;
    ...
}

Composition: (When Foo dies so does Bar)

class Foo
{
    ...
    private Bar bar = new Bar();
    ...
}

Aggregation: (When Foo dies, Bar may live on)

class Foo 
{
    private List<Bar> bars;
}
Howes answered 18/4, 2014 at 16:1 Comment(3)
Association sample just different in multiplicity with Aggregation sample! Bar vs List<Bar>, So do you mean any association with more than one object reference is Aggregation?Goatfish
First of all agregation is a particular case of assotiation. When you use assotiation you just want has-a relationship between Foo and Bar. But if you want has-a + whole-part relationships between Foo and Bar the you use aggregation. You use composition if you want that Foo and Bar have has-a + whole-part + ownership relationship.Howes
@AndritchiAlexei How whole- part relationship affects the code? For has-a it is clear, there is a class member, for ownership relationship it is as well clear, there is an object creator.Cattleya
A
26

The difference between aggregation and composition is pretty fuzzy and AFAIK relates to the logical existence of the "child" objects after the container is destroyed. Hence, in the case of aggregation the objects inside the container can still exist after the container object is destroyed, while in the case of composition design demands that they also get destroyed. Some analogies:

  • A Car object containing four Wheel objects. Normally if you destroy the car (by calling some method to clean it up) you should also destroy the wheels in the process, since it makes little sense for them to exist outside the car (unless you move them to another Car object). More realistically, a reader object wrapping an input stream will also close the inner stream when it gets closed itself. This is composition.
  • A Person object contains (owns) a Radio object. If the Person dies, the Radio may be inherited by another Person i.e. it makes sense for it to exist without the original owner. More realistically, a list holding objects does not demand that all objects get disposed when the list itself is disposed. This is aggregation.

Edit: After reading your links I'd be inclined to go with the first one, since it gives an explanation similar to mine.

Also association is merely invoking a method (sending a message) to another object via a reference to that object.

Alitaalitha answered 26/9, 2012 at 14:40 Comment(2)
@user20358: Association is generally defined as calling a method on another object. It doesn't matter if you keep a reference of not. Technically you can have aggregation without association: just hold some objects but don't use them for anything. :DAlitaalitha
Good examples...very helpfull.Ionia
B
4

If inheritance gives us 'is-a' and composition gives us 'part-of', we could argue that aggregation gives us a 'has-a' relationship.

==> In short, Association, Aggregation, and Composition are terms that represent relationships among objects.

  1. Association: Association means that an object "uses" another object. As you said: "Association is a weaker form of Aggregation where the class doesn't keep a reference to the object it receives."
public class Association
{
    //SomeUtilityClass _obj   /*NO local reference maintained */
    public void doSomething(SomeUtilityClass obj)
    {
       obj.DoSomething();
    }
}
  1. Aggregation:
  • Aggregation is a special type of Association.
  • Aggregation is "has-a" relationship among objects (white diamond)
  • For example, Person 'has-a' Address.
public class Address  
{  
 . . .  
}  
public class Person  
{  
     private Address address;  
     public Person(Address address)  
     {  
         this.address = address;  
     }  
     . . .  
}  

As you can see, the Person does not manage the lifetime of Address. If the Person is destroyed, the Address still exists.

enter image description here

  1. Composition:
  • Composition is a special type of Aggregation, It is a strong type Aggregation.
  • Composition gives us a 'part-of' relationship(dark diamond). Ex: Engine is part-of a car
public class Engine  
{  
 . . .   
}  
public class Car  
{  
    Engine e = new Engine();  
    .......  
}  

enter image description here

Please, prefer the links below to get future information:

Bocage answered 17/6, 2021 at 1:29 Comment(0)
P
1

In Aggregation and Composition you are right but in Association it's not correct. what you suppose about association in your example is dependency in fact.

But what is Association realy?

From Object-Oriented Analysis and Design with Applications By Grady Booch :

Of these different kinds of class relationships, associations are the most general but also the most semantically weak. The identification of associations among classes is often an activity of analysis and early design, at which time we begin to discover the general dependencies among our abstractions. As we continue our design and implementation, we will often refine these weak associations by turning them into one of the other more concrete class relationships

Paugh answered 22/7, 2022 at 18:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.