سؤال

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?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top