Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 433
Merlyn Morgan-Graham Avatar asked Apr 18 '11 02:04

Merlyn Morgan-Graham


People also ask

What's the difference between the dependency injection and service locator patterns?

The main difference is how the dependencies are located, in Service Locator, client code request the dependencies, in DI Container we use a container to create all of objects and it injects dependency as constructor parameters (or properties). Dependency Injection doesn't require the use of a DI Container though.

Is service locator an anti pattern?

Service Locator is a dangerous pattern because it almost works. You can locate Dependencies from consuming classes, and you can replace those Dependencies with different implementations — even with Test Doubles from unit tests.

What is the difference between factory and abstract factory pattern?

Java. The main difference between a “factory method” and an “abstract factory” is that the factory method is a single method, and an abstract factory is an object. The factory method is just a method, it can be overridden in a subclass, whereas the abstract factory is an object that has multiple factory methods on it.

What type of design pattern is Abstract Factory?

The Abstract Factory design pattern is one of the twenty-three well-known GoF design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.


1 Answers

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

  • 'Locates' an existing dependency (the service). Although the service may be created during resolution, it is of no consequence to the Client because:
  • The Client of the Service Locator does NOT take ownership of the dependency.

Factory

  • Creates a new instance of a dependency.
  • The Client of the Factory takes ownership of the dependency.

Abstract Factory

  • Same as a regular Factory except that different deployments may use different implementations of the Abstract Factory allowing different types to be instantiated in those different deployments (you could even change the implementation of the Abstract Factory at runtime but that's not usually how it's used.)
like image 60
Gyan aka Gary Buyn Avatar answered Sep 21 '22 18:09

Gyan aka Gary Buyn