What is the difference between association, aggregation and composition?

后端 未结 19 1750
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 01:44

What is the difference between association, aggregation, and composition? Please explain in terms of implementation.

相关标签:
19条回答
  • 2020-11-22 02:01

    Association is a relationship between two separate classes and the association can be of any type say one to one, one to may etc. It joins two entirely separate entities.

    Aggregation is a special form of association which is a unidirectional one way relationship between classes (or entities), for e.g. Wallet and Money classes. Wallet has Money but money doesn’t need to have Wallet necessarily so its a one directional relationship. In this relationship both the entries can survive if other one ends. In our example if Wallet class is not present, it does not mean that the Money class cannot exist.

    Composition is a restricted form of Aggregation in which two entities (or you can say classes) are highly dependent on each other. For e.g. Human and Heart. A human needs heart to live and a heart needs a Human body to survive. In other words when the classes (entities) are dependent on each other and their life span are same (if one dies then another one too) then its a composition. Heart class has no sense if Human class is not present.

    0 讨论(0)
  • 2020-11-22 02:02

    I think this link will do your homework: http://ootips.org/uml-hasa.html

    To understand the terms I remember an example in my early programming days:

    If you have a 'chess board' object that contains 'box' objects that is composition because if the 'chess board' is deleted there is no reason for the boxes to exist anymore.

    If you have a 'square' object that have a 'color' object and the square gets deleted the 'color' object may still exist, that is aggregation

    Both of them are associations, the main difference is conceptual

    0 讨论(0)
  • 2020-11-22 02:03

    For two objects, Foo and Bar the relationships can be defined

    Association - I have a relationship with an object. Foo uses Bar

    public class Foo { 
        void Baz(Bar bar) {
        } 
    };
    

    Composition - I own an object and I am responsible for its lifetime. When Foo dies, so does Bar

    public class Foo {
        private Bar bar = new Bar(); 
    }
    

    Aggregation - I have an object which I've borrowed from someone else. When Foo dies, Bar may live on.

    public class Foo { 
        private Bar bar; 
        Foo(Bar bar) { 
           this.bar = bar; 
        }
    }
    
    0 讨论(0)
  • 2020-11-22 02:07

    Association

    Association represents the relationship between two classes.It can be unidirectional(one way) or bidirectional(two way)

    for example:

    1. unidirectional

    Customer places orders

    1. bidirectional

    A is married to B

    B is married to A

    Aggregation

    Aggregation is a kind of association.But with specific features.Aggregation is the relationship in one larger "whole" class contains one or more smaller "parts" classes.Conversely, a smaller "part" class is a part of "whole" larger class.

    for example:

    club has members

    A club("whole") is made up of several club members("parts").Member have life to outside the club. If the club("whole") were to die, members("parts") would not die with it. Because member can belong to multiple clubs("whole").

    Composition

    This is a stronger form of aggregation."Whole" is responsible for the creation or destruction of its "parts"

    For example:

    A school has departments

    In this case school("whole") were to die, department("parts") would die with it. Because each part can belong to only one "whole".

    0 讨论(0)
  • 2020-11-22 02:08

    It's important to understand why we should even bother with using more than once relationship line. The most obvious reason is to describe parent-child relationship between classes (when parent deleted all its child’s are deleted as a result), but more impotently, we want to distinguish between simple association and composition in order to place implicit restrictions on the visibility and propagation of changes to the related classes, a matter which plays an important role in understanding and reducing system complexity.

    Association

    The most abstract way to describe static relationship between classes is using the Association link, which simply states that there is some kind of a link or a dependency between two classes or more.

    Weak Association

    ClassA may be linked to ClassB in order to show that one of its methods includes parameter of ClassB instance, or returns instance of ClassB.

    Strong Association

    ClassA may also be linked to ClassB in order to show that it holds a reference to ClassB instance.

    Aggregation (Shared Association)

    In cases where there’s a part-of relationship between ClassA (whole) and ClassB (part), we can be more specific and use the aggregation link instead of the association link, highlighting that ClassB can also be aggregated by other classes in the application (therefore aggregation is also known as shared association).

    It’s important to note that the aggregation link doesn’t state in any way that ClassA owns ClassB nor that there’s a parent-child relationship (when parent deleted all its child’s are being deleted as a result) between the two. Actually, quite the opposite! The aggregation link usually used to stress the point that ClassA is not the exclusive container of ClassB, as in fact ClassB has another container.

    Aggregation v.s. Association The association link can replace the aggregation link in every situation, while aggregation cannot replace association in situations where there’s only a ‘weak link’ between the classes, i.e. ClassA has method/s that contain parameter of ClassB but ClassA doesn’t hold reference to ClassB instance.

    Martin Fowler suggest that the aggregation link should not be used at all because it has no added value and it disturb consistency, Quoting Jim Rumbaugh "Think of it as a modeling placebo".

    Composition (Not-Shared Association)

    We should be more specific and use the composition link in cases where in addition to the part-of relationship between ClassA and ClassB - there’s a strong lifecycle dependency between the two, meaning that when ClassA is deleted then ClassB is also deleted as a result

    The composition link shows that a class (container, whole) has exclusive ownership over other class/s (parts), meaning that the container object and its parts constitute a parent-child/s relationship.

    Unlike association and aggregation, when using the composition relationship, the composed class cannot appear as a return type or parameter type of the composite class. Thus, changes to the composed class cannot propagate to the rest of the system. Consequently, usage of composition limits complexity growth as the system grows.

    Measuring system complexity

    System complexity can be measured simply by looking at a UML class diagram and evaluating the association, aggregation, and composition relationship lines. The way to measure complexity is to determine how many classes can be affected by changing a particular class. If class A exposes class B, then any given class that uses class A can theoretically be affected by changes to class B. The sum of the number of potentially affected classes for every class in the system is the total system complexity.

    You can read more on my blog: http://aviadezra.blogspot.com/2009/05/uml-association-aggregation-composition.html


    0 讨论(0)
  • 2020-11-22 02:08

    Problem with these answers is they are half the story: they explain that aggregation and composition are forms of association, but they don't say if it is possible for an association to be neither of those.

    I gather based on some brief readings of many posts on SO and some UML docs that there are 4 main concrete forms of class association:

    1. composition: A is-composed-of-a B; B doesn't exist without A, like a room in a home
    2. aggregation: A has-a B; B can exist without A, like a student in a classroom
    3. dependency: A uses-a B; no lifecycle dependency between A and B, like a method call parameter, return value, or a temporary created during a method call
    4. generalization: A is-a B

    When a relationship between two entities isn't one of these, it can just be called "an association" in the generic sense of the term, and further described other ways (note, stereotype, etc).

    My guess is that the "generic association" is intended to be used primarily in two circumstances:

    • when the specifics of a relationship are still being worked out; such relationship in a diagram should be converted as soon as possible to what it actually is/will be (one of the other 4).
    • when a relationship doesn't match any of those 4 predetermined by UML; the "generic" association still gives you a way of representing a relationship that is "not one of the other ones", so that you aren't stuck using an incorrect relationship with a note "this is not actually aggregation, it's just that UML doesn't have any other symbol we could use"
    0 讨论(0)
提交回复
热议问题