0

I have been using something like the following code

public void function()
{
OtherClass obj1 = new OtherClass();
OtherClass obj2 = new OtherClass();
}

How can i introduce loose coupling in this code?

DineshxK
  • 11
  • 2

2 Answers2

0

Pass the objects as parameters to the function, or better yet pass the interface that they implement. You would not tie the objects to the function in this way.

Event if this is more about dependency injection then loose coupling, it would not hurt making the change.

Eugene
  • 117,005
  • 15
  • 201
  • 306
  • Yes. **Dependency injection** seems to be a good solution. Thanks – DineshxK Jan 23 '14 at 09:09
  • I used SPRING Dependency injection – DineshxK Jan 23 '14 at 09:30
  • @DineshxK i do not want to be mean, but adding Spring JUST for this is way to much. – Eugene Jan 23 '14 at 09:32
  • Other features like SPRING's support for other frameworks also are helpful to my cause. And loose coupling is a critical requirement for my assignment. Sorry I couldn't explain my full problem in detail. Also am a newbie in IT industry. So plz dont mind my bad expression of the prob. Anyway thanks again – DineshxK Jan 23 '14 at 09:46
0

Move obj1 and obj2 to member variables, and assign them externally from a different class.

Ted Bigham
  • 4,237
  • 1
  • 26
  • 31