• Advertisement
Sign in to follow this  

Can someone explain the Composition model in UML

This topic is 3971 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When is is appropriate to use this model? I am confused... Thanks Chad

Share this post


Link to post
Share on other sites
Advertisement
Composition (line with solid diamond at the composite class end) is used when class A "has a" class B, and the lifetime of an instance of class B is tied to its owning instance of class A.

e.g.

class Element
{
// ...
}

// Composite has a composition relationship with Element
class Composite
{
Element foo; // Created and destroyed with the instance of Composite
}





Aggregation (line with hollow diamond at the aggregate class end) is a form of composition where the lifetime of an instance of class B is not tied to its owning instance of class A.

class Element
{
// ...
}

class Aggregate
{
Element* bar; // Not created or destroyed with the instance of Aggregate
}





A real life (well, sort of) example of composition and of aggregation:

struct Point
{
int x, y;
}

struct TreeNode
{
TreeNode* left, right;
}





This is perhaps a little simplified, but I hope it conveys the idea.

EDIT: Here's the UML itself for the above structures.

UML diagram demonstrating composition and aggregation

EDIT2: That should be "+coordinates", oops.

[Edited by - TheUnbeliever on April 8, 2007 12:51:49 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement