This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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? Please Farouk
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, Sandeep SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)