prograhammer
prograhammer

Reputation: 20630

What are the differences between Object Collections, Object Aggregates, Object Associations, and Object Composition?

I'm seeing these terms used and I'm thinking my own usage of them may be incorrect. I'm wondering exactly how they are different.

Object Collection - ???

Object Aggregation - ???

Object Association - ???

Object Composition - ???

It seems these terms come up lately when talking about ORMs, Data-mappers, and Repositories. For example, Fowler mentions object collections here. What do these terms mean specifically, and how should I use them effectively in speaking?

Upvotes: 3

Views: 142

Answers (2)

prograhammer
prograhammer

Reputation: 20630

To add to umlcat's answer, with some examples:

(as mentioned here in this great post)
Example1: A Company is an aggregation of People. A Company is a composition of Accounts. When a Company ceases to do business its Accounts cease to exist but its People continue to exist.
Example 2: A Text Editor owns a Buffer (composition). A Text Editor uses a File (aggregation). When the Text Editor is closed, the Buffer is destroyed but the File itself is not destroyed.

Also, I would say it is worth noting that People could be moved to be strictly used inside Company (now composition) if you don't care about them ever existing independant of a Company. (Meaning that your application only allows for creation of people from inside a Company context. I don't think PHP lets you actually define classes within classes though. You'll ultimately just have to look at different areas of use in your application to determine if you are using composition or aggregation.)

Upvotes: 0

umlcat
umlcat

Reputation: 4143

Object Collections

Is a more broader term, than "arrays", but, includes "arrays". Collections are objects by themselves, its main goal is to store several other objects, plus other features that depends on each collection kind, such the order of insertion, or order of extraction, if duplicated items are allowed, and so on.

Object Association

Is also, a very generic term, it implies there is a conceptual relationship between 2 objects. There are also, several kinds of "associations", with more specific goals.

Object Aggregation

Is an object association in which an object is related to another object. They can exist independently, and the creation or destruction of one, does not affect the other one, although some operations are result of the interaction of both objects.

Object Composition

Is an object association in which an object is part of another main object. The sub-object cannot exist independently, usually, it's creation or destruction, its managed by the main object.

Note:

Note: Object Aggregation and Object Composition are kinds of Object Association (s), but, there are not the only ones.

Upvotes: 1

Related Questions