Object Orientated Programming - Association,Composition and Aggregation (Multiple Languages)
-
Hi, I've been coding in .NET and Java and C++ for a few years now and I still haven't quite got my head round Association ,Aggregation and Composition. I know a the difference conceptually, eg. A Bank *has* branches (Aggregation), eg. A Child *has* a toy (Composition). The thing that confuses me, is when it comes to coding the relationship. I've looked at examples on the Web and it still confuses me. C# and Java
public class Boy
{
public void PlayWith(Toy toy) {toy.play();} // is this Association?
}public class Bank
{
// private BankBranch branch = new SydneyBranch(); is this Aggregation or Composition
List branches = new List(2) {new SydneyRdBranch(); new ParisRdBranch(); } // is this Aggregation or composition?
}Please could someone explain how to write Association, Aggregation and Composition in Code please. Many Thanks ;) ;) Tom
-
Hi, I've been coding in .NET and Java and C++ for a few years now and I still haven't quite got my head round Association ,Aggregation and Composition. I know a the difference conceptually, eg. A Bank *has* branches (Aggregation), eg. A Child *has* a toy (Composition). The thing that confuses me, is when it comes to coding the relationship. I've looked at examples on the Web and it still confuses me. C# and Java
public class Boy
{
public void PlayWith(Toy toy) {toy.play();} // is this Association?
}public class Bank
{
// private BankBranch branch = new SydneyBranch(); is this Aggregation or Composition
List branches = new List(2) {new SydneyRdBranch(); new ParisRdBranch(); } // is this Aggregation or composition?
}Please could someone explain how to write Association, Aggregation and Composition in Code please. Many Thanks ;) ;) Tom
AFAIK association encompasses both aggregation & composition, the difference between composition and aggregation is that with composition when the containing object goes out of scope then so is the composed object for example a car is composed of an engine once the car goes out of scope the engine goes with it. In contrast aggregation means that the even when the containing object goes out of scope the aggregated object still remains for example a car could have a driver if the car goes out of scope this doesnt mean that the driver object will. It really boils down to whether the object in the contained object is created inside or passed in, your Boy example is Aggregation as the Toy is passed in, and the Bank example is composition as the Branches are created internally hope this helps :)
-
AFAIK association encompasses both aggregation & composition, the difference between composition and aggregation is that with composition when the containing object goes out of scope then so is the composed object for example a car is composed of an engine once the car goes out of scope the engine goes with it. In contrast aggregation means that the even when the containing object goes out of scope the aggregated object still remains for example a car could have a driver if the car goes out of scope this doesnt mean that the driver object will. It really boils down to whether the object in the contained object is created inside or passed in, your Boy example is Aggregation as the Toy is passed in, and the Bank example is composition as the Branches are created internally hope this helps :)
Just explanaing further.. Toy example is Aggregation. As Toy instance is created out side the scope of Class Boy. So deletion of object of Boy will not make impact of object of Toy. Bank has composition relationship with BankBranch. Since instances of BankBranch created inside scop of class Bank so if object of Bank goes out of scope then objects of BankBranch will be get deleted (destructor of Bank must be delete objects of BankBranch)
Akash
-
Just explanaing further.. Toy example is Aggregation. As Toy instance is created out side the scope of Class Boy. So deletion of object of Boy will not make impact of object of Toy. Bank has composition relationship with BankBranch. Since instances of BankBranch created inside scop of class Bank so if object of Bank goes out of scope then objects of BankBranch will be get deleted (destructor of Bank must be delete objects of BankBranch)
Akash
a boy has a hand : composition, there is no hand without the boy. an appartement has a building : composition ---------------- in contrast : a car has a driver: aggregration