What is the difference between association, aggregation and composition?

后端 未结 19 1871
伪装坚强ぢ
伪装坚强ぢ 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:13

        Simple rules:
        A "owns" B = Composition : B has no meaning or purpose in the system 
        without A
        A "uses" B = Aggregation : B exists independently (conceptually) from A
        A "belongs/Have" B= Association; And B exists just have a relation
        Example 1:
    
        A Company is an aggregation of Employees.
        A Company is a composition of Accounts. When a Company ceases to do 
        business its Accounts cease to exist but its People continue to exist. 
        Employees have association relationship with each other.
    
        Example 2: (very simplified)
        A Text Editor owns a Buffer (composition). A Text Editor uses a File 
        (aggregation). When the Text Editor is closed,
        the Buffer is destroyed but the File itself is not destroyed.
    

提交回复
热议问题