码迷,mamicode.com
首页 > 其他好文 > 详细

Association, Composition and Aggregation

时间:2017-10-16 23:29:29      阅读:417      评论:0      收藏:0      [点我收藏+]

标签:target   ini   follow   ops   eve   agg   text   import   href   

An Example of Association, Composition and Aggregation in Java

Here is an example of composition and aggregation, in terms of Java Code. By looking at this code, you can gauge differences between these two. By the way, Composition is also very much preferred in object oriented design over inheritance, even Joshua Bloch has stated its importance in the classic book, Effective Java.

Composition : Since Engine is-part-of Car, the relationship between them is Composition. Here is how they are implemented between Java classes.

public class Car {
    //final will make sure engine is initialized
    private final Engine engine;  
       
    public Car(){
       engine  = new Engine();
    }
}

class Engine {
    private String type;
}



Aggregation : Since Organization has Person as employees, the relationship between them is Aggregation. Here is how they look like in terms of Java classes

public class Organization {
    private List employees;
}
public class Person {
    private String name;   
}





UML Diagram of Association, Composition and Aggregation

UML has different notations to denote aggregation, composition and association.  Association is denoted by the simple arrow while aggregation is denoted by  empty diamond-head arrow and composition is denoted by filled diamond-head arrow. When you draw UML diagram for two related class A and B, where A is associated with B then its denoted by A -> B. Similar way is used to show aggregation and composition between two classes. Here are UML notations for different kind of dependency between two classes.

 

 

技术分享



As I said all three denotes relationship between object and only differ in their strength, you can also view them as below, where composition represents strongest form of relationship and association being the most general form.

技术分享




Association vs Composition vs Aggregation

Here is the list of differences between Composition and Aggregation in point format, for quick review. As I said the key difference between them comes from the point that in the case of Composition, One object is OWNER of another object, while in the case of aggregation, one object is just a USER or another object.

1) If A and B two classes are related to each other such that, B ceased to exist, when A is destroyed, then the association between two objects is known as Composition. An example is Car and Engine. While if A and B are associated with each other, such that B can exist without being associated with A, then this association in known as Aggregation.


2) In the case of Composition A owns B e.g. Person is the owner of his Hand, Mind and Heart, while  in the case of Aggregation, A uses B e.g. Organization uses People as an employee.

3) In UML diagram Association is denoted by a normal arrow head, while Composition is represented by filled diamond arrow head, and Aggregation is represented by an empty diamond arrow head, As shown in below and attached diagram in the third paragraph.

Association  A---->B
Composition  A-----<filled>B
Aggregation  A-----<>B

4) Aggregation is a lighter form of Composition, where a sub-part object can meaningfully exist without main objects.

5) In Java, you can use final keyword to represent Composition. Since in Composition, Owner object expects a part object to be available and functions, by making it final, your provide guarantee that, when Owner will be created, this part object will exist. This is actually a Java idiom to represent a strong form of association i.e. composition between two objects.

6) Another interesting word, which comes handy to understand difference between Composition and Aggregation in software design is "part-of" and "has". If one object is-part-of another object e.g. Engine is part of Car, then association or relationship between them is Composition. On the other hand, if one object just has another object e.g. Car has the driver then it‘s Aggregation.

That‘s all on the difference between Association, Composition and Aggregation in UML, Java and Object oriented design. Since object oriented analysis is more about defining the relationship between object, it‘s important to know what kind of relationship exists between them, composition and aggregation is a two way of representing relationship between two objects.

Association, Composition and Aggregation

标签:target   ini   follow   ops   eve   agg   text   import   href   

原文地址:http://www.cnblogs.com/leslieai/p/7679085.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!