UML: Dependency, Association, Aggregation and Composition

1. Dependency:

        A dependency is a weak relationship between two classes.

        In this example, there is dependency between Point and LineSegment, because LineSegment's draw() operation uses the Point class.

        It indicates that LineSegment has to know about Point, event if it has no attribute of that type.


2. Association:

        Association is a relationship where all objects have their own lifecycle and there is no owner.

        Let's make an example of Teacher and Student. Multiple students can associate with single teacher, and single student can associate with multiple teachers.

        But there is no ownership between the objects and both have their own lifecycle. Both can create/delete independently.

        1> Bi-directional association:

             Associations are bi-directional by default, so both classes know about each other and about the relationship between them.

             In the diagram below, any instance of Student can have 1~* Teachers, and any instance of Teacher can have 1~30 Students.

        2> Uni-directional association:

             Association can also be uni-directional, where one class knows about the other class and the relationship but the other class does not.

             In the diagram below, Customer class knows about any number of Products purchased, but the Product class knows nothing about the Customer.

             Pruduct class doesn't even know about  the existence of Customer class.



3. Aggregation: ("has-a" relationship)

        Aggregation is a specialized form of Association where all objects have their own lifecycle, but there is ownership and child object cannot belong to another parent object[1].

        Association differs from Aggregation only in that Association does not imply any containment.

        A single CPU cannot belong to multiple computers, but if we dismantle the computer, the CPU will not be destroyed.



4. Composition: ("is-a" relationship)

        Composition is again a specialized form(strong type) of Aggregation and we can call this a "death" relationship. It is a strong type of aggregation.

        Child object doesn't have its own lifecycle. And if parent object is deleted, all child object will also be deleted.

        Let's take agagin an example of house and rooms. House can contain multiple rooms, there is no independent lifecycle of room.

 

5. Additional knowledge points:

    1) Generally, it is better to use attributes only for types you would not draw on the diagram, such as primitive types. 

        Diagram below is deprecated.


         Diagram below is encouraged.

    2) Sample



[1]: There are some debates upon the defination of Aggregation. Please refer to Reference Links(1)

 

Reference Links:

1) http://stackoverflow.com/questions/885937/difference-between-association-aggregation-and-composition

2) http://stackoverflow.com/questions/1874049/uml-arrows-pointers-explanation/2293763#2293763

3) http://stackoverflow.com/questions/15487372/bi-directional-and-uni-directional-associations-uml

4) http://www.classdraw.com/help.htm

5) http://msdn.microsoft.com/en-us/library/dd409416(v=vs.100).aspx

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值