• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with Coupling

 
Richa Garg
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

I am having some problem in understang coupling.Please clear my doubts.
I am creating two classes that are in same package.
Variables of Class A are private but method has default access.
I am using that method in another class through its class reference variable . Are they coupled or loosely coupled or tightly coupled.


Code:

Class A{
private int a;
int getA(
return a;
)
}
Class B{
private int b;
public void get y(A a1){
b=a1.getA();
}
}
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richa,

In your example, B is fairly tightly coupled to A.

B's behavior is dependent on A. If A#getA() changes, it affects what happens in B#gety().

At risk of confusing you further, I'd point out this example is generally acceptable under the Law of Demeter, as B#gety() is using "only one dot" of A.

But a better design in this specific example would probably be to refactor B as such:



This decouples B from A.

If B really need to consume data from A to perform its behavior, then it's fine to pass in the object and use the "only one dot" level of coupling. But in this example you give, the coupling doesn't appear to be achieving anything, Which is often the case in the sort of arbitrary examples we see when first learning!
[ May 28, 2008: Message edited by: Stevi Deter ]
 
Jolly Tiwari
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richa ,

Can you share the source of your questions (link to online mock exams)as that would help all ,preparing for the exam and save our time looking for free online mock exams on internet.

Regards

Jolly
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic