The three concepts are closely related, indeed. Aggregation and Composition are actually types of Association.
Association is an 'Has-A' relationship. In Java you can think of any time a class has an member of of a different type, then there is an Association between them. So for example:
Association is an 'Has-A' relationship. In Java you can think of any time a class has an member of of a different type, then there is an Association between them. So for example:
- {
- private final Name name;
- private Costume currentClothes;
- //...
- }
In this case, the Person Has-A name and Has-A Costume, so there is an Association between Person and Name, and Person and Costume.
Aggregation and Composition are specializations of the Association relationship - they are both Has-A relationships, but the difference is the length of that relationship. In the above example, a Person probably has a single Name for the live of the Person, while the Person may have different Costumes depending on the situation. If this is the case then the relationship between Person and Name is Composition, and the relationship between Person and Costume is Aggregation.
Composition is an Association where the containing Object is responsible for the lifetime of the contained Object. To show this in Java Person might have a constructor like this:
Aggregation and Composition are specializations of the Association relationship - they are both Has-A relationships, but the difference is the length of that relationship. In the above example, a Person probably has a single Name for the live of the Person, while the Person may have different Costumes depending on the situation. If this is the case then the relationship between Person and Name is Composition, and the relationship between Person and Costume is Aggregation.
Composition is an Association where the containing Object is responsible for the lifetime of the contained Object. To show this in Java Person might have a constructor like this:
- public class Person
- {
- private final Name name;
- public Person (String fName, String lName)
- {
- name = new Name(fName, lName);
- }
- //...
- }
Typically there would be no 'setName(Name)' methods. The Person is responsible for creating the Name, and when the Person is destroyed then the Name should (usually) be as well.
Aggregation is an Association relationship where the Association can be considered the containing class 'Owning' the contained class, and the lifetime of that relationship is not defined. 'Owning' can be determined as a single-direction Association. In the above example, the Person Has-A Costume, but the Costume would not Have-A Person.More importantly the Person to Costume Association is transient. In Java you can usually tell this because there are setter methods, or other means of adding / replacing the contained Object. So for the Person class you might have:
Aggregation is an Association relationship where the Association can be considered the containing class 'Owning' the contained class, and the lifetime of that relationship is not defined. 'Owning' can be determined as a single-direction Association. In the above example, the Person Has-A Costume, but the Costume would not Have-A Person.More importantly the Person to Costume Association is transient. In Java you can usually tell this because there are setter methods, or other means of adding / replacing the contained Object. So for the Person class you might have:
- public class Person
- {
- private Costume currentClothes;
- public void setClothes(Costume clothes)
- {
- currentClothes = clothes;
- }
- //...
- }
No comments:
Post a Comment