how are association, aggregation and composition written?
I have read some posts about the dif开发者_C百科ferences 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?
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:
- Association: A knows of B, B has meaning in its own right.
- Aggregation: A includes B, B is externally identifiable and may exist on its own merits.
- 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.
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.
As clearly stated in php.net:
An Association is a composition of independently constructed and externally visible parts. When we associate classes or objects, each one keeps a reference to the ones it is associated with.
When we associate classes statically, one class will contain a reference to an instance of the other class.
We can also associate instances at runtime by passing a reference in a constructor (or any other method), which allow us to dynamically change the association relationship between objects.
精彩评论