1) Has-a relationship should never be encapsulated.
2) Has-a relationships can be implemented using instance variables.
3) An array or a collection can be used to implement a one-to-many Has-a relationship.
What about the reasons?
1) 1st one has no sense. Because we can encapsulate data only, not relationship.. OK?
2) What about 2nd..? It seems to be correct, But instance variables???
3) It's correct???
Please confirm??? Thanks in Advanced!
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
1) Well the data is the relationship and it should be encapsulated so that you're programming to an interface rather then direct instance variables.
3) Correct although it would be a zero-to-many relationship
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
You've made me think a lot harder about this. I guess that it depends on what one stands for. If it's referring to the number of Objects in the collection/array then it's zero-to-many (that is what I thought) because the collection/array can be empty. However if it's referring to the relation then it's one. But that is also debatable because A doesn't have a relation with B except for that it's in a collection/array in B. In that case it depends on if the relation is unidirectional or bidirectional.