Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regarding Service Locator Pattern

Service Locator seems like a pretty useful concept and I'd like to implement it in my app. However, I have a few questions about how it should be implemented and how it should be used in conjunction with other programming paradigms.

  1. Should the SL manage the service it is storing? That is, when the SL gets destroyed, should it also destroy the service itself, or should it leave that to whoever registered the service?

  2. What about Dependency Injection? Now that I have an SL is there any point in passing the SL around as a parameter or should I access it globally? After all, much of the whole purpose of DI is covered by SL as well, and I wouldn't like to clutter a lot of my functions with one more parameter.

like image 753
Paul Manta Avatar asked Sep 14 '11 18:09

Paul Manta


People also ask

Is Service Locator an anti pattern?

Service Locator is a well-known pattern, and since it was described by Martin Fowler, it must be good, right? No, it's actually an anti-pattern and should be avoided.

What is the purpose of Service Locator?

The purpose of the Service Locator pattern is to return the service instances on demand. This is useful for decoupling service consumers from concrete classes. An implementation will consist of the following components: Client – the client object is a service consumer.

What is Service Locator pattern in spring?

The service locator design pattern is used when we want to locate various services using JNDI lookup. Considering high cost of looking up JNDI for a service, Service Locator pattern makes use of caching technique. For the first time a service is required, Service Locator looks up in JNDI and caches the service object.

What is a service design pattern?

What are Service Design Patterns? Service design patterns are the basic building blocks that make up services — things like proving your identity, sharing your information, making a payment, or getting a notification.


1 Answers

You would think I wrote this article (I didn't), considering how often I refer to it:

Service Locator is an Anti-Pattern

Dependency Injection makes your dependencies explicit. Service Location buries them in the implementation, and creates a hard dependency on your locator.

like image 193
Phil Sandler Avatar answered Sep 17 '22 23:09

Phil Sandler