Question

I'm a little confused about the Service Locator design pattern.

It seems to be a very, very basic question regarding this pattern, but I couldn't figure it out: What if I have more than one class implementing some IServiceX interface? How the locator would resolve which instance I'm looking for?

For instance, I could register:

ServiceA implementing IServiceX ServiceB implementing IServiceX

It seems perfectly possible, but I'm not sure. Let's suppose I have two kinds of Messaging Service (same service interface) and I want to use both implementations during run-time. How the locator would choose between these different implementations?

Was it helpful?

Solution

That's the fun part. It's entirely up to you to decide.

You may traverse your call stack and figure out where the call is coming from, then depending on the caller, return a different implementation.

You may have a Factory method that always goes for a particular implementation.

You may base the instance you return on the time of day, day of week, amount of memory available, ...

It's all up to you!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top