• Post Reply Bookmark Topic Watch Topic
  • New Topic

basic OOD question : composition  RSS feed

 
Edmund Castermund
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
this question is super simple, but I realize I don't have a great answer to it. If I have a couple classes like so:




what is the best way for the FamilyMember class to be able to access the Family.getName() method?

Should the value be a field in the Family Member constructor? Or should the Family object be passed to the constructor of the FamilyMember object, and also be a field? I've been using the latter method, but wondering if there is a best practice for minimizing coupling.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edmund Castermund wrote:what is the best way for the FamilyMember class to be able to access the Family.getName() method?

Make it a static nested class, viz:of course, then it becomes Family.Member, but, if you think about it, that actually reflects the structure even better.

However, it has to be said that if your getName() method is public, your FamilyMember class would have been able to access it anyway.

Winston
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no relations between two ?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:no relations between two ?

Don't know whether that was directed at me, but if so, and you're asking whether Member should be an inner class: it's an interesting question.

My first inclination is always to make nested classes static, but in this case I think it really comes down to what "member" means. If it only includes direct family, then maybe an inner class would be better; but if "member" also includes cousins, then a single person could be a part of several families, so no.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!