http://www.blackwasp.co.uk/ServiceLocator.aspx
The service locator pattern is a design pattern that is used to decouple a class from its dependencies. Rather than the dependant class instantiating its dependencies directly, they are requested from a centralised service locator object.
What is the Service Locator Pattern?
Object-oriented design can lead to the development of complex class structures with components that are dependant upon other types. If the dependant classes instantiate their dependencies directly they are said to be tightly coupled. This decreases the flexibility of the components and increases the effort required to change functionality and substitute types. In a previous article I described dependency injection design patterns that decouple classes from their dependencies. The service locator design pattern is an alternative approach for promoting loose coupling but does not require injection of dependencies via interfaces, constructors or properties.
The service locator design pattern relies on the creation of a class, called the service locator, that knows how to create the dependencies of other types. Often the service locator acts as a repository for pre-initialised service objects. When one of these is required it is requested using a method of the class. In other situations the service locator methods instantiate objects as they are required, possibly using configuration information passed to the method's parameters.
Implementing the Service Locator Pattern
The UML class diagram above describes one possible implementation of the service locator pattern. There are many other variations on this design pattern. This particular design includes a service locator that is aware of two types of service object. It allows for pre-initialised objects that implement those two interfaces to be configured and returned when requested.
The items in the diagram are described below:
- IService1/2. These interfaces define the available members of the service classes that implement them. The client classes use these interfaces to allow varying implementations to be substituted as required. For example, you may use test stubs or mocks that implement these interfaces for testing purposes.
- Service1/2. These two classes are concrete implementations of the IService1 and IService2 interfaces. These are the types of the objects that will be provided by the service locator.
- ServiceLocator. The ServiceLocator class is responsible for providing implementations of the IService1 and IService2 interfaces at run-time. In this specific design, the two implementations will be pre-initialised using the InitialiseService methods and stored in two private fields. When the GetService methods are called they will return the object from the appropriate field. The class has other members that implement the singleton design pattern to ensure that only one service locator is ever available.
- Initialiser. This class is responsible for initialising the service objects and storing them in the service locator. Various methods for this are available such as direct instantiation during program initialisation or deserializing the objects from a configuration file.
- Client. The Client class is a consumer of the service classes. When it requires one of its dependencies it requests it from the service locator.
The following code shows the basic code of the design pattern implemented using C#:
public
interface
IService1
{
void
SomeMethod();
}
public
interface
IService2
{
void
SomeMethod();
}
public
class
Service1 : IService1
{
public
void
SomeMethod()
{
Console.WriteLine(
"Service1 SomeMethod() called"
);
}
}
public
class
Service2 : IService2
{
public
void
SomeMethod()
{
Console.WriteLine(
"Service2 SomeMethod() called"
);
}
}
public
class
ServiceLocator
{
private
static
ServiceLocator _serviceLocator;
private
static
object
_lockThis =
new
object
();
private
IService1 _service1;
private
IService2 _service2;
private
ServiceLocator() { }
public
static
ServiceLocator GetServiceLocator()
{
lock
(_lockThis)
{
if
(_serviceLocator ==
null
)
_serviceLocator =
new
ServiceLocator();
}
return
_serviceLocator;
}
public
IService1 GetService1()
{
return
_service1;
}
public
IService2 GetService2()
{
return
_service2;
}
public
void
InitialiseService1(IService1 svc)
{
_service1 = svc;
}
public
void
InitialiseService2(IService2 svc)
{
_service2 = svc;
}
}
public
class
Initialiser
{
public
void
InitService1()
{
ServiceLocator.GetServiceLocator().InitialiseService1(
new
Service1());
}
public
void
InitService2()
{
ServiceLocator.GetServiceLocator().InitialiseService2(
new
Service2());
}
}
public
class
Client
{
public
void
UseService1()
{
ServiceLocator.GetServiceLocator().GetService1().SomeMethod();
}
public
void
UseService2()
{
ServiceLocator.GetServiceLocator().GetService2().SomeMethod();
}
}
|