What is the difference between association, aggregation and composition?

后端 未结 19 1868
伪装坚强ぢ
伪装坚强ぢ 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: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".

提交回复
热议问题