Composite and Aggregate and what they imply

Associations between classes are easy enough to understand. Car has 4 wheels.

Car4wheels.png

But in the information system we build, it may be obvious that a Car owns all its Wheels; the car and all its wheels can be looked at as a complete entity of their own –  a composite. If the Car is scrapped, the wheels are also implicitly thrown away. A Composite in UML is created by decorating the association with a filled diamond:

Diamond.png

For any well-behaving MDD tool, this decoration should imply a cascading delete of all associated wheels when the Car is deleted.

A composite also signals to the UML reader that the Car and Wheels are created at the same time and may not be meaningful on their own. So some will argue that the model should be changed from “0..1 Car” to “1 Car” (i.e wheel must always belong to exactly one car):

Diamond 2.png

On the other hand, this might not be the best idea for the domain we are modeling now. If the system we build is one that describes a garbage sorting facility, we may want to say: “Yes, a car often has wheels, and the car and its wheels can be looked at as an entity of their own (a composite), but we sometimes want to take this composite apart and treat the parts separately”. If this is the case, the  aggregate decoration can be used:

Aggregate.png

The aggregation symbol signals to the UML reader that the connection between Car and Wheel is “strong and common” and that “Car owns wheels” is more appropriate to the domain than “Wheel owns Car” (this also applies to the composite symbol).

A well-behaving MDD tool should probably prohibit the deletion of a Car until the Wheels are gone - so that the scrap yard guys do not delete a car while the valuable wheels are still on it.

To sum it up: Composite is stronger than Aggregate. Both symbols imply that the domain sometimes looks at the classes as a bigger unit. The symbols help the UML reader understand the larger compositions in the domain. The symbols imply specific destruction behavior to well-behaving Model-Driven Development Frameworks (MDD-Frameworks).

What Does MDriven Do

Each Association end in MDriven has the “Delete Action” property:

ECO Delete action.png

The DeleteAction can be set to one of these values:

Default.png

DeleteAction on the Wheels Association Calling Car.AsIObject().Delete()
Allow will work even if you have wheels left on the car, but the wheels will be left dangling
Prohibit will not work as long as you have wheels on the car
Cascade will delete any remaining wheels
<Default> If the association end is Composite, treat as Cascade; if the association end is Aggregate, treat as Prohibit; if the association end has aggregation==none, treat as Allow

The recommendation is to leave the DeleteAction on <Default> and use the Aggregation setting to control the delete action AND help UML readers understand the domain.

Deleting Wheels Without the Car

Assuming that your context is the Car:

this.Wheels.delete

would delete the wheels.

Note! Not only unlinking them. If you would like to unlink them, do:

this.Wheels->clear

Note: if you use the aggregation of type Composite, you would create "floating" objects that have a validation error of not having the Car association set.

The MDriven Book - Next Chapter: Derived attributes & associations

This page was edited 51 days ago on 10/02/2024. What links here