문제

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?

도움이 되었습니까?

해결책

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!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top