依赖注入笔记
使用情景
假设当前有一个使用者名叫Bowman,Bowman每天做三件事,看书,玩游戏,抓宠物。
未使用依赖注入(DependencyInjection)的情况
Bowman需要使用一个手机才能做这些事情,在不使用依赖注入的情况下,代码是这样的:
Person类:
namespace Dependency
{
class Person
{
}
}
Bowman类:
namespace Dependency
{
class Bowman : Person
{
private string name;
private int age;
public Bowman()
{
name = "Bowman";
age = 23;
}
Iphone6 iphone6 = new Iphone6(); //直接在Bowman的类当中实例化
public void read ()
{
iphone6.read(name,age);
}
public void play()
{
iphone6.play(name,age);
}
public void grab()
{
iphone6.grab(name,age);
}
}
}
Iphone类:
namespace Dependency
{
class Iphone
{
}
}
Iphone6类:
namespace Dependency
{
class Iphone6 : Iphone
{
public void read(string user, int age)
{
Console.WriteLine(user + " is " + age + " years old" + " and " + "he is reading books now.");
}
public void play(string user, int age)
{
Console.WriteLine(user + " is " + age + " years old" + " and " + "he is playing games now.");
}
public void grab(string user, int age)
{
Console.WriteLine(user + " is " + age + " years old" + " and " + "he is grabing now.");
}
}
}
主函数(函数入口)Program.cs:
namespace Dependency
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hi Bowman,Are you sure to start a new day?");
Console.ReadLine();
Bowman bowman = new Bowman();
Console.WriteLine("Next Step:");
Console.ReadLine();
bowman.read();
Console.ReadLine();
Console.WriteLine("Next Step:");
Console.ReadLine();
bowman.play();
Console.ReadLine();
Console.WriteLine("Next Step:");
Console.ReadLine();
Console.ReadLine();
bowman.grab();
Console.ReadLine();
Console.WriteLine("Bowman's whole day is over.");
}
}
}
首先有一个Iphone6的类继承了Iphone类,Iphone类当中没有任何具体实现,但是Iphone6类当中实现了看书,玩游戏和抓宠物这三个功能。Bowman类当中也没有去做这三个功能的具体实现。此时,我们选择的是在Bowman类当中实例化Iphone6类,然后在相应的方法当中调用Iphone6类的方法。
程序运行的时候实例化Bowman类,并调用Bowman类相应的方法。
依赖注入
上面那种实现的方法有一种弊端,就是Bowman和Iphone6的耦合度过高,在那个程序当中,Bowman如果失去Iphone6就不能实现任何功能。因此我们需要依赖注入,依赖注入就是Bowman不再一定要Iphone6才能实现功能了,开发者设计了手机的功能之后直接把手机塞到Bowman的手里,以此来避免Bowman和手机耦合度过高的问题
以下是使用依赖注入的情况:
Person类:
namespace DependencyInjection
{
class Person
{
}
}
Bowman类:
namespace DependencyInjection
{
class Bowman : Person
{
private string name;
private int age;
private Iphone iphone;
public Bowman(Iphone iphone)
{
name = "Bowman";
age = 23;
this.iphone = iphone;
}
public void read()
{
iphone.read(name, age);
}
public void play()
{
iphone.play(name, age);
}
public void grab()
{
iphone.grab(name, age);
}
}
}
Iphone类:
namespace DependencyInjection
{
class Iphone
{
public virtual void read(string user, int age)
{
Console.WriteLine(user + " is " + age + " years old" + " and " + "he is reading books now.");
}
public virtual void play(string user, int age)
{
Console.WriteLine(user + " is " + age + " years old" + " and " + "he is playing games now.");
}
public virtual void grab(string user, int age)
{
Console.WriteLine(user + " is " + age + " years old" + " and " + "he is grabing now.");
}
}
}
Iphone6类:
namespace DependencyInjection
{
class Iphone6 : Iphone
{
public override void read(string user, int age)
{
Console.WriteLine(user + " is " + age + " years old" + " and " + "he is " +"using iphone6" + " to read books now.");
}
public override void play(string user, int age)
{
Console.WriteLine(user + " is " + age + " years old" + " and " + "he is " + "using iphone6" + " to play games now.");
}
public override void grab(string user, int age)
{
Console.WriteLine(user + " is " + age + " years old" + " and " + "he is " + "using iphone6" + " to grab now.");
}
}
}
Iphone7类:
namespace DependencyInjection
{
class Iphone7 : Iphone
{
public override void read(string user, int age)
{
Console.WriteLine(user + " is " + age + " years old" + " and " + "he is " + "using iphone7" + " to read books now.");
}
public override void play(string user, int age)
{
Console.WriteLine(user + " is " + age + " years old" + " and " + "he is " + "using iphone7" + " to play games now.");
}
public override void grab(string user, int age)
{
Console.WriteLine(user + " is " + age + " years old" + " and " + "he is " + "using iphone7" + " to grab now.");
}
}
}
主函数(函数入口)Program.cs:
namespace DependencyInjection
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hi Bowman,Are you sure to start a new day?");
Console.ReadLine();
Iphone iphone6 = new Iphone6();
Iphone iphone7 = new Iphone7();
Bowman bowman = new Bowman(iphone6);
Console.WriteLine("Next Step:");
Console.ReadLine();
bowman.read();
Console.ReadLine();
Console.WriteLine("Next Step:");
Console.ReadLine();
bowman.play();
Console.ReadLine();
Console.WriteLine("Next Step:");
Console.ReadLine();
Console.ReadLine();
bowman.grab();
Console.ReadLine();
Console.WriteLine("Bowman's whole day is over.");
}
}
}
在这里,Iphone,Iphone6当中都有具体实现,并且新建了一个Iphone7的类(也有具体实现)。在使用依赖注入的时候,开发者直接给Bowman一个Iphone(手机),Bowman不需要去管是什么手机,总之拿来就能用。主函数当中做的事情就是实例化Bowman和Iphone6以及Iphone7,然后随便塞一种Iphone到Bowman手里,让Bowman执行三种功能。这就是依赖注入。
以上使用C#编写