Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how are association, aggregation and composition written?

Tags:

oop

php

I have read some posts about the differences between these 3 relationships and I think I get the point.

I just wonder, are all these written the same when coding?

Question 1: all 3 are just a value of the object type in a instance variable?

class A {
    public $b = ''

    public function __construct($object) {
         $this->b = $object // <-- could be a association, aggregation or a composition relation?
    }
}

Question 2: does it have to be an instance variable or can it be a static one?

class A {
    public static $b = '' // <-- nothing changed?

    public function __construct($object) {
         $this->b = $object
    }
}

Question 3: is there a difference in where the object is created?

I tend to think that composition object is created inside the object:

class A {
    public $b = ''

    public function __construct() {
         $this->b = new Object // is created inside the object
    }
}

and aggregation/association is passed through a constructor or another method:

class A {
    public $b = ''

    public function __construct($object) { // passed through a method
         $this->b = $object
    }
}

Question 4: why/when is this important to know. Do I have to comment an object inside another what relation its about or do you do it in an UML diagram?

Could someone shed a light on these questions?

like image 833
never_had_a_name Avatar asked Apr 24 '10 18:04

never_had_a_name


2 Answers

You are right that these are typically realized as object references, which of course are usually just member fields of some class. This is only because it's natural in an object-oriented system, this can map to other things in different contexts, e.g. foreign keys in a relational database.

As mentioned by @erisco the specifics of the relationship can only be taken in context of the overall model. For example, we can read a composition relationship between a Purchase Order and an Order Line like this (for example): a Purchase Order comprises one or more Order Lines.

I usually interpret the three you give as follows:

  1. Association: A knows of B, B has meaning in its own right.
  2. Aggregation: A includes B, B is externally identifiable and may exist on its own merits.
  3. Composition: A comprises B, B is not externally identifiable or does not have meaning outside of this composition.

That said, I have seen these used to mean pretty much anything that can be dreamed up (and there are very likely people on SO who will disagree with my assessment!) so take care when interpreting someone else's diagrams :-) The above conventions have served me well but YMMV.

Re question 2: A static reference would yield a common association across instances, but it is just one way to implement that, and might be surprising to consumers.

like image 182
Jacob O'Reilly Avatar answered Sep 20 '22 10:09

Jacob O'Reilly


Wikipedia: Class Diagrams probably has your best definitions. None of your examples distinguish between association, aggregation, or composition. Without any definition of what A is, or what B is, there is no definition of what their relationship is. How the reference to the other object is obtained is irrelevant to class diagrams.

like image 24
erisco Avatar answered Sep 21 '22 10:09

erisco