服务定位器,依赖注入(和容器)和控制反转 [英] Service Locator, Dependency Injection (and Container) and Inversion of Control

查看:126
本文介绍了服务定位器,依赖注入(和容器)和控制反转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编程了一段时间,但从来没有兴趣在理论上知道每个概念是什么意思,我可能会使用各种编程概念,但不知道。

I've been programming for some time but never got interested in knowing in theory what each concept means, I may be using a variety of programming concepts, but without knowing it.

服务定位器
对于我而言,指的是快捷方式的记录,以通过减少代码量来加快开发速度。一个问题是:可能的定位器仅引用命名空间/类,或者我可以有变量的注册表?

Service Locator: For me, refers to a record of shortcuts to speed up development by reducing the amount of code. One question is: may Locator refer to namespaces/classes only, or I can have a registry of variables?

这是我对此的理解:

$locator = new ServiceLocator()
$locator->set('app', new System\Application());
$locator->set('db', new System\Pdo());

// Get the objects
$locator->get('db')->connect();
$locator->get('app')->run();

依赖注入(和依赖注入容器)
注入对象内的对象,允许更快地访问这些对象,无论工厂模式如何。和DI容器?

Dependency Injection (and Dependency Injection Container): Injecting objects within objects, allowing faster access to these regardless of the factory pattern. And DI Container?

这是我的理解:

$app = new System\Application(System\Config::load());

反转控制
不明白这个设计模式(或理解,但不知道我所做的是IoC)

Inversion of Control: Don't understand this Design Pattern (or understand but don't know if what I do is IoC)

然后,理论上(最好用简单的例子),这些概念是什么意思?我是正确的,还有什么问题/可以改进吗?

Then, in theory (preferably with simple examples), what does each of these concepts mean? Am I correct, or what is wrong / can be improved?

谢谢!

推荐答案

我认为你正确理解了服务定位器。

I think you understood correctly the Service Locator.

关于依赖注入,意味着如果一个对象有构造函数和/或属性依赖关系,这些被外部注入到对象中,而不是获取依赖关系的对象本身

About Dependency Injection, means that if an object has constructor and/or property dependencies, these are injected in the object by outside, as opposed to the object getting the dependencies by itself

public class MyClass
{
   private $_dep;
   public function __construct($dep=null)
   {
       //$dep has to be injected
       $this->_dep=$dep;                           
   }

   //this is wrong because it couples MyClass to a specific Dependency implementation
   public function __construct()
   {
       $this->_dep=new Dependency();
    }
}
   $dep=new Dependency();
   $obj=new MyClass($dep);

通常,构造函数将抽象(接口)作为参数,并且在类之外实例化具体实现,然后在创建MyClass的新实例时传递给构造函数。

Usually the constructor takes an abstraction (an interface) as the param, and outside the class is instantiated a concrete implementation which then is passed on to the constructor when creating a new instance of MyClass.

一个DI容器自动处理依赖注入。您只需配置它,以便它知道在抽象时被要求返回什么具体的类。容器处理对象创建,通过构造函数和/或属性注入依赖关系。根据容器(我不知道php的一个例子,我只熟悉.net DI容器),你可能还需要注册可以由
创建的对象的类型。

A DI Container, handles the dependency injection automatically. You just configure it, so that it knows what concrete classes to be returned when an abstraction is asked. The Container handles object creation, injecting dependencies via constructor and/or properties. Depending on the container (I don't know an example for php, I'm familiar only with .net DI Containers) you might have to register also the types of objects that can be created by it.

控制反转意味着取决于较低级别(依赖)实现的较高级别类,控件被反转,因此较低级别的类实现取决于更高级别所需的抽象。

Inversion of Control means that instead of a higher level class depending on a lower level class (dependency) implementation, the control is inversed so the lower level class implementation depends on an abstraction required by the higher level class.

//abstraction defined for the use of higher level class
public interface  IRepository {}

// that's the dependency, the lower level class  
public class XmlRepository implements IRepository {}

//the higher level class
 public class MyClass
 {
     public function __construct(IRepository $repo) {}
  }

IoC和DiC一起作为DI Container提供IoC功能。

IoC and DiC go together as the DI Container offers the IoC feature.

这篇关于服务定位器,依赖注入(和容器)和控制反转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆