Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Questions with the wording "appears to" "better than" and other relative quantifier

 
Alexandre Leveille
Greenhorn
Posts: 12
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have difficulties with these two questions from OCP Java SE 6 Programmer Practice Exams from Bert Bates and Kathy Sierra :

1. Coupling.

Code extract:

Choices (not all shown):
B. Class is well encapsulated
C. The setCity is an example of loose coupling

I said B and C. The study guide says B only. I don't get it. Doesn't having a good encapsulation (private variable and using a setter to modify the state of the object) promote loose coupling??. What is wrong here?



2. Encapsulation

Pseud-code:

Choices (not all shown):
A. These classes appear to have low cohesion
E. These classes appear to have weak encapsulation
F. These classes appear to have strong encapsulation

I said A and F. The guide says A and E. Given that there is a get and a set method, as well as other methods to modify the state of these objects, I was thinking as these classes being encapsulated. Anyway, they certainly appear to, given the pseudo-code. Again, what is wrong here?
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alexandre Leveille wrote: Doesn't having a good encapsulation (private variable and using a setter to modify the state of the object) promote loose coupling??. What is wrong here?


Loose coupling is at a higher level than at this variable definition.

Regards,
Dan
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Loose coupling says -

In computing and systems design a loosely coupled system is one in which each of its components has, or makes use of, little or no knowledge of the definitions of other separate components.


Regards,
Dan
 
Himai Minh
Ranch Hand
Posts: 1229
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Coupling is the relationship between two objects.
It is true that encapsulation promotes loose coupling. But in the Contact class example, there is no relationship with other objects.
 
Himai Minh
Ranch Hand
Posts: 1229
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For encapsulation, in this case, it is possible that Payroll class can directly access Employee's info (state). This is a weak encapsulation.
To make it strong encapsulated, Employee should have setInfo and getInfo methods. Payroll should access Employee's info via getInfo method instead accessing Employee's info directly.

In Employee class, it is possible that Employee can directly access the DB. It is possible that some DB configurations (DB URL, DB port number, DB username, password) are exposed to Employee!
Instead, Employee should create a DAO object, which encapsulates DB configurations. Then, Employee access the DB via the DAO object.

Similarly,Utilities class accesses the printer directly and printer configurations are exposed to Utilities. Instead,
Utilities class can access to the printer via a printer manager object, for example. This printer manager object encapsulates the printer's configuration.

This is just one way to implement it.
 
Alexandre Leveille
Greenhorn
Posts: 12
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your answers. I still think these questions are really subject to a debate, but I should be good to understand what they expect on the exam.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic