20
On relationships between classes, what is the difference between Association, Aggregation and Composition?
20
On relationships between classes, what is the difference between Association, Aggregation and Composition?
28
The definitions are conceptual. In practice the important thing is that we have generic associations between objects and the most commonly used term is composition, even when in fact it is not one, formally speaking. Some small details will make it one or the other. What matters is trying to keep the coupling as low as possible. In this regard the simple association is the most appropriate... when feasible.
Interestingly the terms are used to structure data but it is the algorithms that will define the real difference.
An example is the relationship between a teacher and students. A student may have several teachers and a teacher may have several students. One does not depend on the other to exist. Teachers can exist without students and students can exist without teachers (at least in normal requirements). Another example:
class Cliente {
var contatos = new List<Contato>();
AdicionarContato(Contato contato) {
contatos.Add(contato); //este contato independe deste cliente
}
}
An example is the relationship between teachers and departments. Departments can have multiple teachers. And the teacher can only be linked to a single department. But they are independent. A teacher can exist without a link with a department and it does not depend on teachers to exist. Another example:
class Cliente {
var notas = new List<NotaFiscal>();
EmitirNota(NotaFiscal nota) {
notas.Add(nota); //a nota existe sempre mesmo que o cliente não exista mais
}
}
An example is the relationship between a university and departments. Apart from the university having several departments, they can only exist if the university exists. There is a dependency. Another example:
class NotaFiscal {
var itens = new List<item>();
AdicionarItem(Produto produto, int quantidade) {
itens.Add(new Item(produto, quantidade); //este item só existe nesta nota
}
}
The terms have this relationship:
I found out later that there’s already one question that answers in detail about the specific types which are more important. It’s not enough to be exact duplicate.
3
summarizing:
in association we have no owner. the objects have own life time. and the Child Objects are independent. or there is some kind of hierarchical relationship and objects can be called independently from each other
in aggregation we have only a single owner. the objects have independent life time. and the Child Objects belong to a single Parent. that is, there is a kind of hierarchical link to the unilateral type. although the objects are still minimally independent
composition is identical to aggregation but life time is that of the owner. ie. if the owner is stopped. or others are also
http://www.codeproject.com/Articles/330447/Understanding-Association-Aggregation-and-Composit
Browser other questions tagged oop terminology
You are not signed in. Login or sign up in order to post.
Related: "Composition and aggregation: what differences and how to use?"
– mgibsonbr