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

猜你喜欢

转载自davyjones2010.iteye.com/blog/2088712
今日推荐