• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is that an association?

 
Costa Lamona
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

if I am having a class1 that USE class2 without having instance variable pointing to class2, Is that Association?

if class1 is having a instance variable which reference class2, while class1 never instantiated class2, is this Aggregation?

if the previous case but class1 instantiated class2, is this composition.

if you want to give me a link, that will be great but please answer the question first.


Thanks
 
Glen Ihrig
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Costa,

I am pretty new to this Composition, Association, Aggregation business myself, but after a lengthy study of "Head First OOA&D" and "The Object-Oriented Thought Process" and many internet articles, I have come the following conclusions regarding your questions:

if I am having a class1 that USE class2 without having instance variable pointing to class2, Is that Association?


Yes, while the classes are clearly associated, the instance of class2 is independent and may participate in relationships with other objects. This is true as long as class2 is instantiated elsewhere. It make no difference how class1 accesses class2.

if class1 is having a instance variable which reference class2, while class1 never instantiated class2, is this Aggregation?


No. This is still Association. Aggregation is not clearly defined beyond the UML symbol (white diamond). I (and notable others) believe you are best ignoring Aggregation, it doesn't really exist...

if the previous case but class1 instantiated class2, is this composition.


Yes. The key here is that class1 "owns" this instance of class2, the life span of these instances are the same. They are truly composed into a new whole.

For more detail, and examples, see this thread

Differing opinions are welcome, but please read the linked thread first.

Regards,
-Glen
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://faq.javaranch.com/java/AssociationVsAggregationVsComposition
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic