Is the Service Locator pattern any different from the Abstract Factory pattern?
At first glance, the Service Locator pattern looks the same as the Abstract Factory pattern to me. They both seem to have the same use (you query them to receive instances of abstract services), and they both have been mentioned when I read about Dependency Injection.
However, I have seen the Service Locator pattern described as a poor idea, but have seen direct support for the Abstract Factory pattern in at least one major Dependency Injection framework.
If they aren't the same, what are the differences?
I have stumbled across the same question while investigating these patterns. I think the major differences can be found between a Service Locator and a Factory (whether it is abstract or not):
Service Locator
Factory
Abstract Factory
From what I have read so far, I think the difference is:
The Service Locator pattern
The Abstract Factory pattern
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."
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().
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". Is worth to mention that Gamma Abstract Factory example violate Interface Segregation Principle (ISP) stated by Martin. ISP and SOLID in general are more moderns principles or maybe for simplicity where omitted.
Gamma and Martin's works precede Seemann's, so I think he should follow definition already made.
While Fowler propose Service Locator as a way to implement Dependency Inversion, Seemann consider it as an anti-pattern. Neither Gamma or Martin mention Service Locator.
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. This configuration step is not mentioned by Martin or Gamma in their definition of 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.
链接地址: http://www.djcxy.com/p/77754.html上一篇: 如何将此代码转换为现在使用依赖注入模式?
下一篇: 服务定位符模式与抽象工厂模式有什么不同?