Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. Object Orientated Programming - Association,Composition and Aggregation (Multiple Languages)

Object Orientated Programming - Association,Composition and Aggregation (Multiple Languages)

Scheduled Pinned Locked Moved Design and Architecture
csharpc++javatutorialquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tom Moore
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • T Tom Moore

      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

      S Offline
      S Offline
      stavinski
      wrote on last edited by
      #2

      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 :)

      M 1 Reply Last reply
      0
      • S stavinski

        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 :)

        M Offline
        M Offline
        Member 2767084
        wrote on last edited by
        #3

        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

        H 1 Reply Last reply
        0
        • M Member 2767084

          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

          H Offline
          H Offline
          Hasan Jaffal
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups