The problem here is tight coupling in classes. In other words, "class A depends on class B". Let's take an example and try to understand the tight coupling.

Suppose we have a Samsung class that contains an Android class object. An issue with this is tight coupling between classes. In other words the Samsung class depends on the Android object. So if the Android class changes for any reason then it may affect the Samsung class.

The problem is that the Samsung class controls the creation of the Android object.

The Android class is directly referenced in the Samsung class that leads to tight coupling among address and customer objects.

If we make any changes to the Android class then it might affect the Samsung class also, because the Samsung class is dependent on the Android class. Suppose that we add some properties or methods to the Android class then it might require changes in the Samsung class.

In easier words, this is called Object Dependency. Object Dependency means that, for one object to work, it needs another object. In other words one object is dependent on another object.

Look at the image given below to understand the concept of tight coupling.

Dependency Injection

To solve the problem of tight coupling here we have an easier and simpler way, "Inversion of Control" (IOC).

In more simple words Inversion of Control could be defined as: "delegation of a task of object's creation to a third party, to do low coupling between objects and to minimize dependency between objects".

There are two points related to IOC:

The main class shouldn't be dependent on another class. Both classes should be dependent on abstraction.

Abstraction could be done through an interface or abstract class.

This is the basic concept for implementing the Inversion of Control (IOC) pattern.

It wipes out tight coupling between objects.

Makes objects and application more flexible.

It facilitates creating more loosely coupled objects and their dependencies.

Kindly have a look at the code below to understand the IOC implementation. I am using Constructor Injection.

Here the object reference would be passed to the constructor of the business class Samsung. In this case, since the Samsung class depends on the Android class, a reference of the Android class will pass steps to implement Constructor Injection. I am using a very basic approach for this.

Step 1

Create an Interface:

interfaceIMobile

{ }

Step 2

Implement an interface to the Android class. An object of the Android class references the Samsung class. See:

classAndroid : IMobile

{

privatestring strAndriodVersion;

privateint strInternalMemory;

publicstring propAndriodVersion

{

get { return strAndriodVersion; }

set { strAndriodVersion = value; }

}

publicint propInternalMemory

{

get { return strInternalMemory; }

set { strInternalMemory = value; }

}

public Android(string AndriodVersion, int InternalMemory)

{

propAndriodVersion = AndriodVersion;

propInternalMemory = InternalMemory;

}

publicoverridestring ToString()

{

returnstring.Format(" Version :{0} is having internal memory:{1}", this.propAndriodVersion, this.propInternalMemory);

}

}

Step 3

Make a reference of the interface in the Samsung class:

classSamsung

{

privatestring MobileName;

privateIMobile objIMobile;

public Samsung(string strMobileName, IMobile obj)

{

this.MobileName = strMobileName;

this.objIMobile = obj;

}

publicoverridestring ToString()

{

returnthis.MobileName;

}

}

Step 4

Create a third party class, that creates an instance of all these objects. See:

Hi Folks, I am Sachin Kalia .I work in .Net technologies and work in software company in 3 Pillar Global(Earlier Brickred Technologies).I share my thoughts about .Net and associated technologies through C#Corner .I ... Read more