javablogspot

Just another WordPress.com weblog

Java Term of the Week: Composition

Posted by damuchinni on February 22, 2009

Composition takes the aggregation relationship one step further. I like to think of composition as aggregation with responsibilities. It ensuring that the containing object is responsible for the lifetime of the object it holds. If Object B is contained within Object A, then Object A is responsible for the creation and destruction of Object B. Unlike aggregation, Object B cannot exist without Object A.

For example, imagine a Student object that uses a GregorianCalenger object to hold the student’s date of birth. If the Student object is responsible for the creation and destruction of the GregorianCalender object then the relationship between the two classes is composition.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: