Service Locator Pattern
The service locator pattern is a design pattern used in software development to encapsulate the processes involved in obtaining a service with a strong abstraction layer. This pattern uses a central registry known as the “service locator” which on request returns the information necessary to perform a certain task.
The ServiceLocator is responsible for returning instances of services when they are requested for by the service consumers or the service clients.
UML Diagram Service Locator Pattern
- Service Locator : The Service Locator abstracts the API lookup services, vendor dependencies, lookup complexities, and business object creation, and provides a simple interface to clients. This reduces the client’s complexity. In addition, the same client or other clients can reuse the Service Locator.
- InitialContext : The InitialContext object is the start point in the lookup and creation process. Service providers provide the context object, which varies depending on the type of business object provided by the Service Locator’s lookup and creation service.
- ServiceFactory : The ServiceFactory object represents an object that provides life cycle management for the BusinessService objects. The ServiceFactory object for enterprise beans is an EJBHome object.
- BusinessService : The BusinessService is a role that is fulfilled by the service the client is seeking to access. The BusinessService object is created or looked up or removed by the ServiceFactory. The BusinessService object in the context of an EJB application is an enterprise bean.
In the above diagram, ClassA has compile time dependencies on ServiceA and ServiceB.But this situation has drawbacks.
- If we want to replace or update the dependencies we must change the classes source code and recompile the solution.
- The concrete implementation of the dependencies must be available at compile time.
By using the Service Locator pattern :
In simple words, Service Locator pattern does not describe how to instantiate the services. It describes a way to register services and locate them.
Let’s see an example of Service Locator Pattern.
Creating a new ServiceOne object Executing ServiceOne Creating a new ServiceTwo object Executing ServiceTwo Returning cached ServiceOne object Executing ServiceOne Returning cached ServiceTwo object Executing ServiceTwo
- Applications can optimize themselves at run-time by selectively adding and removing items from the service locator.
- Large sections of a library or application can be completely separated. The only link between them becomes the registry.
- The registry makes the code more difficult to maintain (opposed to using Dependency injection), because it becomes unclear when you would be introducing a breaking change.
- The registry hides the class dependencies causing run-time errors instead of compile-time errors when dependencies are missing.
The following strategies are used to implement service Locator Pattern :
- EJB Service Locator Strategy : This strategy uses EJBHome object for enterprise bean components and this EJBHome is cached in the ServiceLocator for future use when the client needs the home object again.
- JMS Queue Service Locator Strategy : This strategy is applicable to point to point messaging requirements. The following the strategies under JMS Queue Service Locator Strategy.
- JMS Queue Service Locator Strategy
- JMS Topic Service Locator Strategy
- Type Checked Service Locator Strategy : This strategy has trade-offs. It reduces the flexibility of lookup, which is in the Services Property Locator strategy, but add the type checking of passing in a constant to the ServiceLocator.getHome() method.