aspose file tools*
The moose likes Associate Certification (SCJA,OCAJ 5/6) and the fly likes Line has two points: asociation or composition? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "Line has two points: asociation or composition?" Watch "Line has two points: asociation or composition?" New topic
Author

Line has two points: asociation or composition?

santiago martin alfageme
Greenhorn

Joined: Apr 27, 2007
Posts: 9
On McKenzie's Study Guide there is a great example about geometry classes in order to illustrate hieracy and abstraction. I specially enjoyed the reading because I work with geographical information systems (GIS), so I find these explanations particulary useful.
At page 181 stays an example of a Line Class design just to explain association: A Line 'has' two points. Inside the constructor of a Line there is a checking (really an if) to prevent the introduction of null objects as points as parameters.
Thinking about ...... It wouldn't be better to design the relationship between Line and Point as a composition?: a line can't exist without two points. Just like this:


I don't have experience in programming Java, so I can't guess the drawbacks of this approach.
Thanks in advance.


Santiago Martin Alfageme
Spain
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

I guess it depends on the difference between the definition of composition and association. What is the difference?

-Cameron McKenzie
santiago martin alfageme
Greenhorn

Joined: Apr 27, 2007
Posts: 9
With association it's is possible a line with only a point. That´s the reason of your checking. With composition no line can exist without two points. Besides if one point is deleted, linked line too.
Regards
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

I think I like your logic!

Indeed, it is the difference between the strong concept of something being 'composed of' something else, as opposed to it being loosely associated.

With the exam, you'll need to know how associations and compositions differ with regards to UML diagrams. However, how they actually differ in the implementation of the code is slightly beyond the scope of the exam, from what I recall.

Keep posting!

-Cameron McKenzie
santiago martin alfageme
Greenhorn

Joined: Apr 27, 2007
Posts: 9
OK. All ist clear. Anyway, sometimes it is good to 'touch' Object Oriented (OO) concepts in the way of implementation. At least for me. I´ve heard for a long time buzzwords like hieracy, polymorfism, and so on, Also I´ve learned how to read UML. But only translating pretty OO ideas into code I fix the subject. By the way SCJA preparation books like yours help a lot in this way.
Regards.
M Castell
Greenhorn

Joined: Mar 22, 2010
Posts: 1
I think that it would be better if the relationship between Line and Point was designed as a composition. A composition association is a strong relationship where the internal object only makes conceptual sense while stored in the containing object. The containing object also has the responsibility of managing the life cycle of the internal object.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

A composition association is a strong relationship where the internal object only makes conceptual sense while stored in the containing object.


But two points in space exist, regardless of whether there is a line that connects them. Furthermore, a single point could be used by many different lines, so a point should have a lifecycle that exists beyond the drawing or erasing of any lines. So, that would tend to go against the argument of composition, and lend weight to the idea that it is an association.

-Cameron McKenzie

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Line has two points: asociation or composition?