• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Line has two points: asociation or composition?

 
santiago martin alfageme
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess it depends on the difference between the definition of composition and association. What is the difference?

-Cameron McKenzie
 
santiago martin alfageme
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic