简体   繁体   English

服务定位器和工厂设计模式之间有什么区别?

[英]What's the difference between the Service Locator and the Factory Design pattern?

我正在使用unity,并且正在创建一个包装它的类,但我不知道如何调用它(服务定位器或工厂),它们都封装了对象的创建,所以...有什么区别?

A factory creates objects for you, when requested. 根据要求,工厂会为您创建对象。

Service locator returns objects that may already exist, that is services that may already exist somewhere for you. 服务定位器返回可能已经存在的对象,即对于您某处可能已经存在的服务。

Just think about the meaning of the names: 只要考虑一下名称的含义:

  • Factory: is a place where objects are created. 工厂:是创建对象的地方。
  • Service: is something that can do something for you as a service. 服务:是一项可以为您提供服务的服务。
  • Service locator: is something that can find something that can perform a service. 服务定位器:可以找到可以执行服务的对象。

Actually there is a clear separation between this both pattern. 实际上,这两种模式之间存在明显的分隔。 It's common known that both pattern are used for avoid dependencies from concrete types. 众所周知,两种模式都用于避免来自具体类型的依赖。

However after read 但是看完之后

Some severe contradictions arises: 出现一些严重的矛盾:

Seemann said: "An Abstract Factory is a generic type, and the return type of the Create method is determined by the type of the factory itself. In other words, a constructed type can only return instances of a single type." Seemann说:“抽象工厂是泛型类型,Create方法的返回类型由工厂本身的类型确定。换句话说,构造类型只能返回单个类型的实例。”

While Rober C. Martin didn't mention anything about generic types and furthermore, factory example in his book allow to create instance of more than one type of objects distinguish between them using a key string as parameter in the Factory.Make(). 尽管Rober C. Martin没有提及泛型类型,而且,他的书中的工厂示例允许使用Factory.Make()中的键字符串作为参数来创建一种以上类型的对象的实例,以区分它们。

Gamma said that intent of Abstract Factory is to "Provide an interface for creating families of related or dependent objects without specifying their concrete classes". Gamma表示,Abstract Factory的目的是“提供一个用于创建相关或相关对象族的接口,而无需指定其具体类”。 Is worth to mention that Gamma Abstract Factory example violate Interface Segregation Principle (ISP) stated by Martin. 值得一提的是,Gamma抽象工厂示例违反了Martin所说的接口隔离原则(ISP)。 ISP and SOLID in general are more moderns principles or maybe for simplicity where omitted. ISP和SOLID通常是更现代的原理,或者为简单起见省略了。

Gamma and Martin's works precede Seemann's, so I think he should follow definition already made. Gamma和Martin的作品先于Seemann的作品,所以我认为他应该遵循已经做出的定义。

While Fowler propose Service Locator as a way to implement Dependency Inversion, Seemann consider it as an anti-pattern. 尽管Fowler提出将Service Locator用作实现依赖倒置的一种方法,但Seemann认为它是一种反模式。 Neither Gamma or Martin mention Service Locator. Gamma或Martin均未提及服务定位器。

However, Seemann and Fowler agreed in that Service Locator needs a configuration step to register an instance of a concretes class, that instance is what will be later returned when an object of that kind be requested. 但是,Seemann和Fowler同意,“服务定位器”需要配置步骤来注册具体类的实例,该实例是稍后在请求此类对象时将返回的实例。 This configuration step is not mentioned by Martin or Gamma in their definition of Abstract Factory. Martin或Gamma在他们的Abstract Factory定义中未提及此配置步骤。 Abstract Factory pattern suppose a new object to be instantiated every time an object of that kind be requested. 抽象工厂模式假设每次请求此类对象时都要实例化一个新对象。

Conclusion 结论

The main difference between Service Locator and Abstract Factory is that Abstract Factory suppose a new object be instantiated an returned at each requested and Service Locator needs to be configured with an object instance and every time the same instance will be returned. Service Locator和Abstract Factory之间的主要区别在于Abstract Factory假定实例化一个新对象,并在每个请求的对象处返回一个对象,并且Service Locator需要配置一个对象实例,并且每次都将返回该实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM