前端之家收集整理的这篇文章主要介绍了
理解依赖注入 for Zend framework 2,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
依赖注入(Dependency Injection),也称为控制反转(Inversion of Control),一种设计模式,其目的是解除类之间的依赖关系。
假设我们需要举办一个Party,Party需要主持人、厨师、灯光、音响、食品、酒水等等。那么Party对他们存在依赖关系。用程序语言表示如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
@H_301_12@ //Party.PHP
class Party{
//主持人
private $_host;
function __construct(){
include "./Host.PHP";
$this->_host = new Host();
}
function startParty(){
$this->_host->sayHello();
}
}
//Host.PHP
class Host{
private $_name;
function sayHello(){
echo "My name is " . $this->_name;
}
}
//main
$party = new Party();
$party->startParty();
可见Party的运行依赖于Host,没有Host,Party不能单独运行,也不能单独发布为组件。为了解除Party对Host的依赖,我们可以这么做:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
@H_301_12@ //Party.PHP
class Party{
//主持人
private $_host;
function __construct($host = ""){
if($host){
$this->_host = $host;
}
}
function startParty(){
$this->_host->sayHello();
}
function setHost($host){
$this->_host = $host;
}
}
//Host.PHP
class Host{
private $_name;
function sayHello(){
echo "My name is " . $this->_name;
}
}
//main
$host = new Host();
$party = new Party();
$party->setHost($host);//或者 $party = new Party($host)
$party->startParty();
此时Party类对Host类的依赖被移到外面,运行时Host类通过构造函数或者setter注入到Party中。Party本身可以被单独发布。如果Host没有sayHello方法,将其注入到Party中必然导致异常。为了约束Host必须含有sayHello方法,可以使用接口。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
@H_301_12@ //Party.PHP
class Party{
//主持人
private $_hostInterface;
function __construct($host = ""){
if($host){
$this->_hostInterface = $host;
}
}
function startParty(){
$this->_hostInterface->sayHello();
}
function setHost($host){
$this->_hostInterface = $host;
}
}
//HostInterface.PHP
Interface HostInterface{
public function sayHello();
}
//Host.PHP
class Host implement HostInterface{
private $_name;
function sayHello(){
echo "My name is " . $this->_name;
}
}
//main
$host = new Host();
$party = new Party();
$party->setHost($host);//或者 $party = new Party($host)
$party->startParty();
这么做实际上已经达到了解耦的目的,那么下面我要把Party所有依赖的厨师、灯光、音响、食品、酒水都加进去,会是怎样?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
@H_301_12@ //Party.PHP
class Party{
//主持人
private $_host;
private $_cooker;
private $_wine;
private $_food;
private $_music;
private $_light;
function __construct(){
}
function startParty(){
$this->_host->sayHello();
}
function setHost($host){
$this->_host = $host;
}
function setCooker($cooker){
$this->_cooker = $cooker;
}
function set Wine($wine){
$this->_wine = $wine;
}
//...等等 food,light,music
}
]
//Host.PHP
class Host{
private $_name;
function sayHello(){
echo "My name is " . $this->_name;
}
}
//Cooker.PHP
class Cooker{}
class Wine{}
clsas Light{}
//...等等其他类
//main
$host = new Host();
$cooker = new Cooker();
$wine = new Wine();
//...等等
$party = new Party();
$party->setHost($host);
$party->setCooker($cooker);
$party->setWine($wine);
$party->setFood($food);
$part->setMusic($music);
$part->setLight($light);
$party->startParty();
代码中大量的实例化和setter调用,是否可以优化?我们需要一个DI容器,在DI中管理各个类,由DI负责注入。此时的DI更像是一个“大工厂模式”。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
@H_301_12@ //Party.PHP
class Party{
private $_di;
function __construct(){
}
function startParty(){
$this->_di->get("Host")->sayHello();
$this->_di->get("Cooker")->cook();
//...
}
function setDI($di){
$this->_di = $di;
}
}
//HostInterface.PHP
Interface HostInterface{
public function sayHello();
}
//Host.PHP
class Host implement HostInterface{
private $_name;
function sayHello(){
echo "My name is " . $this->_name;
}
}
//main
$di = new DI();
//匿名函数形式
$di->set("Host", function(){
return new Host();
});
//类名
$di->set("Cooker", "Path\To\Cooker.PHP");
//直接返回实例
$di->set("Wine", new Path\To\Wine.PHP);
//数组
$di->set("Light", array("className" => "Path\To\Light"));
$party = new Party();
$party->setDI($di);
$party->startParty();
DI使用set来注册服务类,注册的方式可以有很多种,他们都是惰性实例化,set时并不实例化,在get时才会实例化。而DI在注册服务类时通常会使用配置来实现,如JSON、XML或者PHP数组。
DI非常注重约定,$di->get(“Host”)获取的实例如果不是Host实例的话,将会引发异常。因此,DI属于强约定模式,通常用于底层架构,Zend framework 2的核心部分使用DI模式,但在框架应用层使用服务定位模式(ServiceLocator),服务定位模式与依赖注入非常相似,都能够解除类之间的依赖关系,且实现思路与DI基本一致。
参考资料:
@L_
502_0@
话说 依赖注入(DI) or 控制反转(IoC)
用PHP实现简单的控制反转(IOC) 依赖注入(DI),用JSON配置文件
——————————————–
本站除标注[FW]和资讯
文章外都为原创
文章,转载请注:
转载来源:
Coming X
原文
链接:
理解依赖注入 for Zend framework 2