user3811839
user3811839

Reputation: 141

UML Can a relationship be composition AND aggregation?

I have a situation where class A has a B and cannot exist without the B, but B can exist completely independent from the A.

From the perspective of A this is a composition relationship, but from the perspective of B this is an aggregate relationship.

What do I do??

Upvotes: 2

Views: 247

Answers (3)

Gerd Wagner
Gerd Wagner

Reputation: 5673

Your abstract example is either an aggregation or a composition, where the parts are separable (in the sense that they do not existentially depend on the aggregate). You didn't make any statement about the exclusivity of your B parts. If they cannot be shared among two or more A's, then your association between A and B is a composition.

Notice that it's a widespread mistake to think that composition is defined as an aggregation where the composite existentially depends on the component. The UML Specification states that this is typically, but not necessarily, the case for compositions. Rather what defines a composition is that the components are exclusive parts that are not shared with other composites.

See also my answer to a related SO question.

Upvotes: 0

Bruce
Bruce

Reputation: 2310

In common usage, an association would not be both composition and aggregation and it depends on the application. A taxi/tire relationship would be composition with the black filled diamond adjacent to the taxi. The tire can exist in a parts department independent of the taxi, but a taxi cannot be dispatched without 4 tires. The parts department can exist without tires so that would be aggregation.

Upvotes: 0

Vladimir
Vladimir

Reputation: 2129

If A is composed of B, it is association with composed end on A class side. Composition and aggregation are types of association ends. It is possible to have composition on the one side and aggregation on the other side.

You can identify composition by answering question.. is Class A composed of class B ? If yes, it is composition on class A side... example: Is computer composed of memory, CPU etc ? All computer components are in composition with computer.

Example for aggregation: particular chair is placed in room. It is aggregation, because the room is not composed of chair (room is composed of walls, window etc.). But chair can be in composition with furniture placed in room.

Upvotes: 1

Related Questions