What is the difference between Association, Aggregation and Composition in OOP?

Asked

Viewed 10,490 times

20

On relationships between classes, what is the difference between Association, Aggregation and Composition?

2 answers

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.

  • To association between two objects occurs when they are completely independent of each other but eventually are related. It can be considered a relation of many to many. There is no property (Ownership) or dependence between them. The relationship is occasional.

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
    }
}

Associação

  • To aggregation is an association but there is an exclusivity and certain objects can only relate to a specific object. It’s a one-to-many relationship. An object is the owner of others but there is no dependency, so both can exist even if the relationship is not established. In fact there are controversies about the exact definition and what is more important, the relationship of one to many or property.

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
    }
}

Agregação

  • To composition is an aggregation that has dependency between objects, that is, if the main object is destroyed, the objects that compose it can no longer exist. There’s the call death relation.

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
    }
}

Composição

The terms have this relationship:

Relação

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

You are not signed in. Login or sign up in order to post.