I'm using unity and I'm creating a class that wrapps it and I dont' know how to call it, service locator or factory, both encapsulate the creation of the objects, so.... what's the difference?
-
Does this answer your question? [Is the Service Locator pattern any different from the Abstract Factory pattern?](https://stackoverflow.com/questions/5698026/is-the-service-locator-pattern-any-different-from-the-abstract-factory-pattern) – Michael Freidgeim Feb 03 '23 at 22:54
3 Answers
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.

- 23,796
- 16
- 59
- 82
-
The service at the end is a class, that do something. To retrieve it it has to be created so.... the Service Locator is like a factory, because it has to make a "new" – Fritjof Berggren Nov 30 '11 at 14:26
-
3It does not have to create a new service object... instead the service object will be registered before trying to locate the service, could be when the application starts, so that in the future the service locator can return that service to a caller. – Miguel Angelo Nov 30 '11 at 17:22
-
-
the only missing is interface name `IServiceLocator` stands for locating, `IServiceFactory` stands for creation, but it's possible to have `ServiceLocator: IServiceFactory`. `IServiceFactory` has a method `GetService(name)`. The implementation can lookup injected services, or it can create new thus Locator vs Factory, but how I name Interface to be neutral? `IServiceProvider`? – Pawel Cioch Mar 28 '19 at 21:56
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
- Agile Software Development, Principles, Patterns, and Practices [book] by Rober C. Martin
- Inversion of Control Containers and the Dependency Injection pattern [article] by Martin Fowler at http://martinfowler.com/articles/injection.html
- Pattern Recognition: Abstract Factory or Service Locator? [article] by Mark Seemann at http://blog.ploeh.dk/2010/11/01/PatternRecognitionAbstractFactoryorServiceLocator/
- Design Pattern [book] by Erich Gamma et al
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.

- 477
- 5
- 8
Read this from Mark Seemann
http://blog.ploeh.dk/2010/11/01/PatternRecognitionAbstractFactoryOrServiceLocator.aspx http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx
It's good for the begining

- 15,723
- 5
- 46
- 73