본문 바로가기

StackOverflow

[소프트웨어 공학] Association, Aggregation, Composition 에 대한 설명

Q: Association, Aggregation, Composition 이 세가지의 차이가 무엇인가요? 구현 시 어떻게 적용되는지 설명 해주세요. (질문자: ???)



A: 
Association: 모든 오브젝트가 각자의 라이프사이클을 가지고 있고, 어떤 오브젝트가 다른 오브젝트를 소유하지는 않은 경우입니다.

예를 들면, 선생님과 학생의 경우를 들 수 있습니다. 많은 학생이 한 선생님에게 Association를 가질 수 있고, 한 학생이 여러 선생님에게 Association를 가질 수도 있습니다. 하지만 이 관계에는 누가 누구를 소유하거나 하지는 않습니다. 각자가 생성/소멸을 독립적으로 합니다.


Aggregation: Association의 특별한 경우인데, 모든 오브젝트가 각자의 라이프사이클을 가지고 있으며, 한 오브젝트가 다른 오브젝트를 소유하고 있는 경우입니다.

선생님이 어떤 부서에 Aggregation되어 있다고 합시다. 소속된 관계이기 때문에, 한 선생님이 여러 부서에 Aggregation될 수는 없습니다. 그렇다고 해서 부서가 소멸될 때 선생님도 소멸되는 것은 아닙니다. 이것을 "has-a" 관계라고 할 수 있습니다.


Composition: Aggregation의 특별한 경우인데, 이것을 "죽음의" 연관관계라고 부를 수도 있습니다. 강력하게 연관된 Aggregation이며, 자식 오브젝트는 자신의 라이프사이클을 가지지 않고, 부모 오브젝트가 소멸될 경우 자식 오브젝트도 함께 소멸됩니다.

이 관계는 집과 그 안의 방 사이의 관계라 할 수 있습니다. 집은 여러 개의 방을 가지고 있고, 방은 절대로 독립적인 라이프사이클을 가질 수 없습니다. 우리가 집을 소멸시키면, 방도 함께 소멸될 것입니다.

또 다른 예로, 문제와 선택지의 관계를 들 수 있습니다. 한 문제는 여러 개의 선택지를 가질 수 있고, 선택지는 여러 문제에 속할 수 없습니다. 문제를 소멸시킬 때, 선택지도 함께 소멸될 것입니다. (역주: 이 예는 틀릴 수도 있습니다. 여러 선택지와 여러 문제가 함께 있을수도 있으니까요!)



(답변자:Varun Gupta)

_


_

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

shareeditflag
_


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

    Let’s take 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 be created and deleted independently.

  • Aggregation is a specialised form of Association where all objects have their own lifecycle, but there is ownership and child objects can not belong to another parent object.

    Let’s take an example of Department and teacher. A single teacher can not belong to multiple departments, but if we delete the department, the teacher object will not be destroyed. We can think about it as a “has-a” relationship.

  • Composition is again specialised form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object does not have its lifecycle and if parent object is deleted, all child objects will also be deleted.

    Let’s take again an example of relationship between House and Rooms. House can contain multiple rooms - there is no independent life of room and any room can not belong to two different houses. If we delete the house - room will automatically be deleted.

    Let’s take another example relationship between Questions and Options. Single questions can have multiple options and option can not belong to multiple questions. If we delete the questions, options will automatically be deleted.

shareeditflag