Open In App

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



Design components



Suppose classes with dependencies on services whose concrete types are specified at compile time.

In the above diagram, ClassA has compile time dependencies on ServiceA and ServiceB.But this situation has drawbacks.

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.




// Java program to
// illustrate Service Design Service
// Locator Pattern
 
import java.util.ArrayList;
import java.util.List;
 
// Service interface
// for getting name and
// Executing it.
 
interface Service {
    public String getName();
    public void execute();
}
 
// Service one implementing Locator
class ServiceOne implements Service {
    public void execute()
    {
        System.out.println("Executing ServiceOne");
    }
 
    @Override
    public String getName()
    {
        return "ServiceOne";
    }
}
 
// Service two implementing Locator
class ServiceTwo implements Service {
    public void execute()
    {
        System.out.println("Executing ServiceTwo");
    }
 
    @Override
    public String getName()
    {
        return "ServiceTwo";
    }
}
 
// Checking the context
// for ServiceOne and ServiceTwo
class InitialContext {
    public Object lookup(String name)
    {
        if (name.equalsIgnoreCase("ServiceOne")) {
            System.out.println("Creating a new ServiceOne object");
            return new ServiceOne();
        }
        else if (name.equalsIgnoreCase("ServiceTwo")) {
            System.out.println("Creating a new ServiceTwo object");
            return new ServiceTwo();
        }
        return null;
    }
}
 
class Cache {
    private List<Service> services;
 
    public Cache()
    {
        services = new ArrayList<Service>();
    }
 
    public Service getService(String serviceName)
    {
        for (Service service : services) {
            if (service.getName().equalsIgnoreCase(serviceName)) {
                System.out.println("Returning cached "
                                   + serviceName + " object");
                return service;
            }
        }
        return null;
    }
 
    public void addService(Service newService)
    {
        boolean exists = false;
        for (Service service : services) {
            if (service.getName().equalsIgnoreCase(newService.getName())) {
                exists = true;
            }
        }
        if (!exists) {
            services.add(newService);
        }
    }
}
 
// Locator class
class ServiceLocator {
    private static Cache cache;
 
    static
    {
        cache = new Cache();
    }
 
    public static Service getService(String name)
    {
        Service service = cache.getService(name);
 
        if (service != null) {
            return service;
        }
 
        InitialContext context = new InitialContext();
        Service ServiceOne = (Service)context.lookup(name);
        cache.addService(ServiceOne);
        return ServiceOne;
    }
}
 
// Driver class
class ServiceLocatorPatternDemo {
    public static void main(String[] args)
    {
        Service service = ServiceLocator.getService("ServiceOne");
        service.execute();
 
        service = ServiceLocator.getService("ServiceTwo");
        service.execute();
 
        service = ServiceLocator.getService("ServiceOne");
        service.execute();
 
        service = ServiceLocator.getService("ServiceTwo");
        service.execute();
    }
}

Output:

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

Advantages :

Disadvantages :

Strategies

The following strategies are used to implement service Locator Pattern :


Article Tags :