aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes IBM test Question - Coupling and encapsulation 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 » Engineering » OO, Patterns, UML and Refactoring
Bookmark "IBM test Question - Coupling and encapsulation" Watch "IBM test Question - Coupling and encapsulation" New topic

IBM test Question - Coupling and encapsulation

Farouk Mohamed1
Ranch Hand

Joined: Jul 26, 2001
Posts: 113
In design #1, the Catalog object has a getProducts() method, which returns a collection object, such as a Dictionary or array, containing all the Products the company sells. In design #2, the Catalog object has a getProductNumbered(anIdentifier) method, which returns the Product with the specified unique identifier. Considering the objects returned, which of the following BEST characterizes the two designs?
1. Both designs maintain the objects' encapsulation and reduce coupling by accessing state data via methods only and not directly.
2. Both the designs break the objects' encapsulation, adding brittle coupling
3. Design #1 breaks the encapsulation of the Catalog, adding brittle coupling. Design #2 maintains the encapsulation of the Catalog, making future design changes easier.
Select only one?
Please can any one explain how encapsulation and coupling is looked into for the 2 designs and reason out why they have chosen the proper statement?
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi Farouq,
This has been discussed before in the forum.Search for the "Coupling question" and you will find a huge discussion between me and Junilu Lacar!
If you are considering this question from the perspective of what consitutes a good design or a bad design, I should say both the designs are bad.Hence the answer should be 2.This is because both the designs are exposing the Product object to the client.You should never allow this.The Client should only ask the Catalog about a price of the Product and the Catalog should give the Client this information without exposing the Product object to the client.
However, to get 100% in the pre-assessment test (and also to select the answer on the basis of objects returned) the correct answer is 3.
Design#1 will allow the client to play with the Collection of Products, thereby giving a chance to the client to make the Product objects in the Collection object to be out of sync with what is being managed by the Catalog.Hence encapsulation of the Catalog is under threat.Also, coupling is high since the Catalog is managing the lifecycle of the Product.
Design#2 will maintain encapsulation of the Catalog.This is because although the Catalog method is giving away the Product object to the client, the Catalog in this case is not managing the life-cycle of the Products.The method, getProductNumbered(anidentifier) will delegate this responsibility to one of the helper classes to get the Product object.

Hope this helps,
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)

<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
I agree. Here's the link:
subject: IBM test Question - Coupling and encapsulation