Inversion of control , Dependency Inversion Principle(DIP) And Dependency Injection

public class A
{
B b;

public A()
{
b = new B();
}

public void Task1() {
// do something here..
b.SomeMethod();
// do something here..
}

}

public class B {

public void SomeMethod() {
//doing something..
}
}
public class A
{
B b;

public A()
{
b = Factory.GetObjectOfB ();
}

public void Task1() {
// do something here..
b.SomeMethod();
// do something here..
}
}

public class Factory
{
public static B GetObjectOfB()
{
return new B();
}
}
public interface IB
{
string SomeMethod();
}
public class B implements IB
{
public B()
{
}

public string SomeMethod() {
return "Dummy Customer Name";
}
}
public class Factory
{
public static IB SomeMethod()
{
return new B();
}
}
public class A
{
IB b;

public A()
{
b = Factory.SomeMethod();
}

public string someMethod()
{
return b.SomeMethod();
}
}
public class A
{
IB b;

public A(IB b)
{
this.b = b;
}

public A()
{
b = new B();
}

public string ProcesData()
{
return b.someMethod();
}
}

public interface IB
{
string someMethod();
}

public class B implements IB
{
public B()
{
}

public string someMethod()
{
return "Dummy Customer Name";
}
}
public class C
{
A a;

public C()
{
this.a = new A(new B());
}

public string someMethod() {
return a.someMethod();
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store