Design Patterns - SPLessons

Service Locator Pattern

Chapter 34

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Service Locator Pattern

Service Locator Pattern

shape Description

Service Locator Pattern – Utilize a Service Locator item to digest all JNDI utilization and to conceal the complexities of introductory connection creation, EJB home article query, and EJB object re-creation. Different customers can reuse the Service Locator article to decrease code many-sided quality, give a solitary purpose of control, and enhance execution by giving a storing office.

This pattern reduces the customer multifaceted nature that outcomes from the customer’s reliance on and need to perform query and creation forms, which are asset serious. To dispense with these issues, this example gives a component to extract all conditions and system points of interest into the Service Locator.

shape Example

Following is an example for the Service Locator Pattern.
Service.java

public interface Service {
   public String getName();
   public void execute();
}

Service Locator Pattern – Here just created the interface called Service and given the getName method.
Service1.java

public class Service1 implements Service {
   public void execute(){
      System.out.println("Executing Service1");
   }
   @Override
   public String getName() {
      return "Service1";
   }
}

Service 1 and Service 2 are the concrete classes.
Service2.java

public class Service2 implements Service {
   public void execute(){
      System.out.println("Executing Service2");
   }
   @Override
   public String getName() {
      return "Service2";
   }
}

Service Locator Pattern – Service 1 and Service 2 are the concrete classes.
InitialContext.java

public class InitialContext {
   public Object lookup(String jndiName){
   
      if(jndiName.equalsIgnoreCase("SERVICE1")){
         System.out.println("Looking up and creating a new Service1 object");
         return new Service1();
      }
      else if (jndiName.equalsIgnoreCase("SERVICE2")){
         System.out.println("Looking up and creating a new Service2 object");
         return new Service2();
      }
      return null;		
   }
}

Service Locator Pattern – The InitialContext object is the begin point in the query and creation process. Administration suppliers give the setting object, which shifts relying upon the sort of business item gave by the Service Locator’s query and creation administration. A Service Locator that gives administrations to numerous sorts of business items uses various sorts of connection protests, each got from an alternate supplier.
Cache.java

import java.util.ArrayList;
import java.util.List;
public 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);
      }
   }
}

The Cache to store references of services to reuse them.
ServiceLocator.java

public class ServiceLocator {
   private static Cache cache;
   static {
      cache = new Cache();		
   }
   public static Service getService(String jndiName){

      Service service = cache.getService(jndiName);

      if(service != null){
         return service;
      }
      InitialContext context = new InitialContext();
      Service service1 = (Service)context.lookup(jndiName);
      cache.addService(service1);
      return service1;
   }
}

The Service Locator abstracts the API query administrations, merchant conditions, query complexities, and business object creation, and gives a basic interface to customers. This lessens the customer’s intricacy. What’s more, the same customer or different customers can reuse the Service Locator.
ServiceLocatorPatternDemo.java

public class ServiceLocatorPatternDemo {
   public static void main(String[] args) {
      Service service = ServiceLocator.getService("Service1");
      service.execute();
      service = ServiceLocator.getService("Service2");
      service.execute();
      service = ServiceLocator.getService("Service1");
      service.execute();
      service = ServiceLocator.getService("Service2");
      service.execute();		
   }
}

This class will have main method. The ServiceFactory object speaks to an object that gives life cycle administration to the BusinessService objects. The ServiceFactory object for big business beans is an EJBHome object. The ServiceFactory for JMS segments can be a JMS ConnectionFactory object.
Output
Result will be as follows.

Looking up and creating a new Service2 object
Executing Service2
Returning cached  Service1 object
Executing Service1
Returning cached  Service2 object
Executing Service2

Summary

shape Key Points

  • Service Locator Pattern provides Uniform Service Access to Clients.
  • Service Locator Pattern improves Network Performance
  • Service Locator Pattern improves Client Performance by Caching