The design pattern, Service Locator is an important part in software development and it is core J2EE Design Patterns. Looking up for a service is one of the core features of service locator. A robust abstraction layer performs this function. The design pattern uses a central registry called Service Locator. When it is required to locate a number of services, through JNDI lookup, the service locator pattern is used.
The service locator pattern uses caching techniques in order to lookup JNDI for a certain service. This way, it reduces the overall looking up cost. When a service is needed to be looked up firstly, the service locator rummages in the JNDI. Then, then it catches the object. The lookups for services (or the same service) in the future is done by Service Locator in the cache. It ultimately enhances the working of the application.
Let’s see the following class diagram for the Service Locator Pattern.
Use a Service Locator to implement and encapsulate service and component lookup. A Service Locator hides the implementation details of the lookup mechanism and encapsulates related dependencies.
also read:
There are certain entities involved in the service locator design pattern; service, context or initial context, service locator, cache, and client.
The service locator pattern adds the code during the runtime, the app does not need to be recompiled. In some cases, you don’t even need to restart is.
Let’s see the following example for implementation of the Service Locator Design Pattern.
BusinessService.java
/** * */ package com.doj.patterns.j2ee.servicelocator; /** * @author Dinesh.Rajput * */ public interface BusinessService { public String getServiceName(); public void executeService(); }
OperationBusinessService.java
/** * */ package com.doj.patterns.j2ee.servicelocator; /** * @author Dinesh.Rajput * */ public class OperationBusinessService implements BusinessService { @Override public String getServiceName() { return "Operation"; } @Override public void executeService() { System.out.println("Executing Business Service for Operation Team"); } }
ClientBusinessService.java
/** * */ package com.doj.patterns.j2ee.servicelocator; /** * @author Dinesh.Rajput * */ public class ClientBusinessService implements BusinessService { @Override public String getServiceName() { return "Client"; } @Override public void executeService() { System.out.println("Executing Business Service for Client"); } }
InitialContext.java
/** * */ package com.doj.patterns.j2ee.servicelocator; /** * @author Dinesh.Rajput * */ public class InitialContext { public Object lookup(String jndiName){ if(jndiName.equalsIgnoreCase("OPERATION")){ System.out.println("Looking up and creating a new OperationBusinessService object"); return new OperationBusinessService(); }else if (jndiName.equalsIgnoreCase("CLIENT")){ System.out.println("Looking up and creating a new ClientBusinessService object"); return new ClientBusinessService(); } return null; } }
ServicesCache.java
/** * */ package com.doj.patterns.j2ee.servicelocator; import java.util.ArrayList; import java.util.List; /** * @author Dinesh.Rajput * */ public class ServicesCache { private List businessServices; public ServicesCache(){ businessServices = new ArrayList<>(); } public BusinessService getBusinessService(String serviceName){ for (BusinessService businessService : businessServices) { if(businessService.getServiceName().equalsIgnoreCase(serviceName)){ System.out.println("Returning cached " + serviceName + " object"); return businessService; } } return null; } public void addBusinessService(BusinessService newBusinessService){ boolean exists = false; for (BusinessService businessService : businessServices) { if(businessService.getServiceName().equalsIgnoreCase(newBusinessService.getServiceName())){ exists = true; } } if(!exists){ businessServices.add(newBusinessService); } } }
ServiceLocator.java
/** * */ package com.doj.patterns.j2ee.servicelocator; /** * @author Dinesh.Rajput * */ public class ServiceLocator { private static ServicesCache servicesCache; static { servicesCache = new ServicesCache(); } public static BusinessService getBusinessService(String jndiName){ BusinessService businessService = servicesCache.getBusinessService(jndiName); if(businessService != null){ return businessService; } InitialContext context = new InitialContext(); BusinessService businessService2 = (BusinessService)context.lookup(jndiName); servicesCache.addBusinessService(businessService2); return businessService2; } }
ServiceLocatorPatternDemo.java
/** * */ package com.doj.patterns.j2ee.servicelocator; /** * @author Dinesh.Rajput * */ public class ServiceLocatorPatternDemo { /** * @param args */ public static void main(String[] args) { BusinessService businessService = ServiceLocator.getBusinessService("OPERATION"); businessService.executeService(); businessService = ServiceLocator.getBusinessService("CLIENT"); businessService.executeService(); businessService = ServiceLocator.getBusinessService("OPERATION"); businessService.executeService(); businessService = ServiceLocator.getBusinessService("CLIENT"); businessService.executeService(); } }
Looking up and creating a new OperationBusinessService object Executing Business Service for Operation Team Looking up and creating a new ClientBusinessService object Executing Business Service for Client Returning cached OPERATION object Executing Business Service for Operation Team Returning cached CLIENT object Executing Business Service for Client
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…