Registered: Nov 2001
First, as is common in this industry, I think the question confuses the design idea of "information hiding" and the implementation concept of "encapsulation." I've encountered pretty good agreement among my peers that there's a difference between the concepts. Check out this article: http://www.javaworld.com/javaworld/...psulation.html.
But, since the term used is "encapsulation", that's the term I'll use for clarity.
I think the question doesn't include enough information to really make a definitive answer.
Here's my take, first just from the encapsulation stand point:
Answer a) seems true, but without knowing the origin of the object's returned, it's hard to be sure. If design #1 returns a private collection of products for the client to access willy-nilly, THAT is definately a violation of encapsulation. It could, in fact, return a copy (clone) of a private collection. This would support encapsulation.
Answer b) seems false, but again, without knowing more about where the objects returned really come from it's hard to say for sure.
Answer c) seems possibly true or false, for the reason's I've stated above.
In short, I don't think much can be said about violating encapsulation without more details.
Now, on to coupling:
I believe design #1 has higher coupling that design #2 because clients have access to ALL products whether they need them or not, but that doesn't make #1 a bad design -- just a less desireable one. If it were me, I'd be more inclined to return an iterator rather than the entire collection IF I had to allow clients to access the entire product catalog (say, for displaying it in some UI). If, after more consideration, I found my clients were mostly getting the entire collection of products and then searching through it for one product, design #2 would be a worth-while, highly adviseable improvement.
Design #2 reduces coupling mainly because it narrows the information the client accesses in one call.
You could infer that design #1 and design #2 resulted from addressing the same design problem: access to the catalog's products, and if that's the case, design #2 would be a better choice. I guess if pressed, I'd have to pick c) because design #2 hides (encapsulates) more than #1, but I wouldn't be surprised if a) was also the answer. Frankly, I think the test author was looking for the answer c).
Report this post to a moderator | IP: Logged