理解依赖注入 for Zend framework 2

来源:互联网 发布:工信部 数据分析 编辑:程序博客网 时间:2024/05/14 16:09

依赖注入(Dependency Injection),也称为控制反转(Inversion of Control),一种设计模式,其目的是解除类之间的依赖关系。

假设我们需要举办一个Party,Party需要主持人、厨师、灯光、音响、食品、酒水等等。那么Party对他们存在依赖关系。用程序语言表示如下:

1234567891011121314151617181920212223242526
//Party.phpclass Party{    //主持人    private $_host;     function __construct(){        include "./Host.php";        $this->_host = new Host();    }     function startParty(){        $this->_host->sayHello();    }} //Host.phpclass Host{    private $_name;    function sayHello(){        echo "My name is " . $this->_name;    }} //main$party = new Party();$party->startParty();

可见Party的运行依赖于Host,没有Host,Party不能单独运行,也不能单独发布为组件。为了解除Party对Host的依赖,我们可以这么做:

123456789101112131415161718192021222324252627282930313233
//Party.phpclass Party{    //主持人    private $_host;     function __construct($host = ""){        if($host){            $this->_host = $host;        }    }     function startParty(){        $this->_host->sayHello();    }     function setHost($host){        $this->_host = $host;    }} //Host.phpclass 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方法,可以使用接口。

1234567891011121314151617181920212223242526272829303132333435363738
//Party.phpclass Party{    //主持人    private $_hostInterface;     function __construct($host = ""){        if($host){            $this->_hostInterface = $host;        }    }     function startParty(){        $this->_hostInterface->sayHello();    }     function setHost($host){        $this->_hostInterface = $host;    }} //HostInterface.phpInterface HostInterface{    public function sayHello();} //Host.phpclass 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所有依赖的厨师、灯光、音响、食品、酒水都加进去,会是怎样?

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
//Party.phpclass 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.phpclass Host{    private $_name;    function sayHello(){        echo "My name is " . $this->_name;    }}//Cooker.phpclass 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更像是一个“大工厂模式”。

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
//Party.phpclass Party{    private $_di;     function __construct(){    }     function startParty(){        $this->_di->get("Host")->sayHello();        $this->_di->get("Cooker")->cook();        //...    }     function setDI($di){        $this->_di = $di;    }} //HostInterface.phpInterface HostInterface{    public function sayHello();} //Host.phpclass 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基本一致。

参考资料:
理解PHP 依赖注入
话说 依赖注入(DI) or 控制反转(IoC)
用PHP实现简单的控制反转(IOC) 依赖注入(DI),用JSON配置文件
——————————————–
本站除标注[FW]和资讯文章外都为原创文章,转载请注:
转载来源: Coming X
原文链接: 理解依赖注入 for Zend framework 2
0 0
原创粉丝点击