Sign in to follow this  
chadsxe

Can someone explain the Composition model in UML

Recommended Posts

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this