Design Pattern Service Locator Pattern - Design Patterns

What is Design Pattern Service Locator Pattern?

The facility locator design pattern is use when we want to locate different services using JNDI lookup. In view of high cost of looking up JNDI for a service, Service Locator pattern makes use of caching technique. For the first time a service is necessary, Service Locator looks up in JNDI and caches the service object. Auxiliary lookup or same service via Service Locator is completed in its cache which improves the concert of application to great level. Following are the entity of this kind of design pattern.

  • Service - Actual Service which will process the request. Reference of such service is to be looked upon in JNDI server.
  • Context / Initial Context - JNDI Context carries the reference to service used for lookup purpose.
  • Service Locator - Service Locator is a single point of contact to get services by JNDI lookup caching the services.
  • Cache - Cache to store references of services to reuse them
  • Client - Client is the object that invokes the services via Service Locator.

Implementation

We are going to create a ServiceLocator, InitialContext, Cache, and Service as a choice of substance representing our entities.Service1 and Service2 represent existing services.

ServiceLocatorPatternDemo, our presentation class, is performing as a client here and will use Service Locator to display Service Locator Design Pattern.
servicelocator_pattern_uml_diagram

Step 1

Create Service interface.

Service.java

Step 2

Create concrete services.

Service1.java

Service2.java

Step 3

Create Initial Context for JNDI lookup

InitialContext.java

Step 4

Create Cache

Cache.java

Step 5

Create Service Locator

ServiceLocator.java

Step 6

Apply the Service Locator to display Service Locator Design Pattern.

ServiceLocatorPatternDemo.java

Step 7

Verify the output.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Design Patterns Topics