注册表模式与服务定位器模式Vs依赖注入容器 [英] Registry pattern Vs Service locator pattern Vs Dependency Injection Container

查看:286
本文介绍了注册表模式与服务定位器模式Vs依赖注入容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它们之间是否有区别,而不是通过键设置和获取数组中的对象?

Is there any difference between them rather than set and get objects in an array by key?

class Registry
{
private $container=array();
  public static function Set($name,$object){
    self::$container[$name]=$object;
  }
  public static function &Get($name){
  return self::$container[$name];
  }
}


推荐答案

注册表模式



注册表模式是用于查找只知道其名称的对象的模式。此模式在内部存储对象的实例,并使用字典映射来检索这些实例。

Registry Pattern

Registry pattern is a pattern used to lookup an object knowing only its name. This pattern stores instances of objects internally and uses a dictionary mapping to retrieve those instances later.

一个DI容器包含一个具有对象类型映射到抽象的注册表。它是更高级的,因为当对象被解析时,它被实例化,所有对象的依赖关系也是如此。

A DI container contains a registry which has a mapping of object types to abstractions. It is more advanced in that when an object is resolved it is instantiated, and so are all of the object's dependencies.

当您从DI容器请求对象时,您将从您请求的对象开始获取 对象图 作为根。每个依赖对象都通过递归地遍历每个类的构造函数自动注入,从没有依赖关系的类开始,并使用注册表作为引导实例化每个对象。

When you request an object from a DI container, you get an object graph starting with the object you request as the root. Each dependent object is injected automatically by recursively going through the constructor of each class, starting at the classes that have no dependencies and instantiating each object using the registry as a guide.

依赖注入是一种模式不一定使用DI容器。 DI模式由位于入口点的组合根组成的应用程序。组合根是注册类型的地方,并且 根对象图 之间的实例化。一旦实例化了根对象,应用程序就会自行运行。应用程序本身没有参考DI容器,并没有紧密耦合。

Dependency Injection is a pattern that doesn't necessarily use a DI container. The DI pattern is comprised of a composition root which sits at the entry-point of the application. The composition root is where the types are registered and where the root object graph is instantiated. Once the root object is instantiated, the application runs on its own. The application itself has no reference to the DI container and is not tightly coupled to it.

服务定位器被许多人认为是反模式。这个想法是,您可以将容器注入到对象中,或者在运行时使用DI容器的静态引用创建实例。

Service locator is considered by many people to be anti-pattern. The idea is that you either inject the container into your object or use a static reference to the DI container to create instances at runtime.

主要区别在于应用程序明确依赖开启(因此紧密耦合到)DI容器。

The primary difference is that the application is explicitly dependent on (thus tightly-coupled to) the DI container.

使用服务定位器的另一个缺点是因为你正在注入DI容器,所以不可能从类构造函数中看到它所依赖的接口。您需要查阅文档或分析源代码来确定一个类的依赖关系。

Another disadvantage of using Service Locator is that because you are injecting the DI container, it is not possible to see from the class constructors what interfaces it is dependent on. You instead have to consult the documentation or analyze the source code to determine what a class's dependencies are.

虽然被认为是反模式,但仍然有一些情况是有道理的使用。然而,在所有其他选项(环境背景,物业注入等)用尽之后,应该被认为是最后的手段。

Although considered anti-pattern, there are still some situations where it makes sense to use. However, it should be considered a last resort after all other options (ambient context, property injection, etc.) have been exhausted.

这篇关于注册表模式与服务定位器模式Vs依赖注入容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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