The Service Locator pattern (Service Locator Pattern) is used when we want to use JNDI queries to locate various services. Considering the high cost of finding JNDI for a service, the service locator pattern makes full use of caching technology. When a service is first requested, the service locator looks for the service in JNDI and caches the service object. When the same service is requested again, the service locator looks in its cache, which greatly improves the performance of the application. The following are the entities of this design pattern.
服务(Service) -the service that actually processes the request. References to this service can be found in the JNDI server.
Context / 初始的 Context -JNDI Context comes with a reference to the service you are looking for.
服务定位器(Service Locator) -Service locator is a single point of contact to obtain services through JNDI lookup and caching services.
缓存(Cache) -caches references to storage services in order to reuse them.
客户端(Client) -Client is the object that invokes the service through ServiceLocator.
6.34.1. Realize ¶
We will create ServiceLocator 、 InitialContext 、 Cache 、 Service As various objects that represent entities. Service1 And Service2 Represents an entity service.
ServiceLocatorPatternDemo Class is here as a client and will use the ServiceLocator To demonstrate the service locator design pattern.
6.34.2. Step 1 ¶
Create the service interface Service.Service.java ¶
publicinterfaceService{publicStringgetName();publicvoidexecute();}
6.34.3. Step 2 ¶
Create an entity service.Service1.java ¶
publicclassService1implementsService{publicvoidexecute(){System.out.println("Executing
Service1");}@OverridepublicStringgetName(){return"Service1";}}
Service2.java ¶
publicclassService2implementsService{publicvoidexecute(){System.out.println("Executing
Service2");}@OverridepublicStringgetName(){return"Service2";}}
6.34.4. Step 3 ¶
Create an InitialContext for the JNDI query.InitialContext.java ¶
publicclassInitialContext{publicObjectlookup(StringjndiName){if(jndiName.equalsIgnoreCase("SERVICE1")){System.out.println("Looking
up and creating a new Service1
object");returnnewService1();}elseif(jndiName.equalsIgnoreCase("SERVICE2")){System.out.println("Looking
up and creating a new Service2
object");returnnewService2();}returnnull;}}
6.34.5. Step 4 ¶
Create a cache Cache.Cache.java ¶
importjava.util.ArrayList;importjava.util.List;publicclassCache{privateList<Service>services;publicCache(){services=newArrayList<Service>();}publicServicegetService(StringserviceName){for(Serviceservice:services){if(service.getName().equalsIgnoreCase(serviceName)){System.out.println("Returning
cached"+serviceName+"object");returnservice;}}returnnull;}publicvoidaddService(ServicenewService){booleanexists=false;for(Serviceservice:services){if(service.getName().equalsIgnoreCase(newService.getName())){exists=true;}}if(!exists){services.add(newService);}}}
6.34.6. Step 5 ¶
Create a service locator.ServiceLocator.java ¶
publicclassServiceLocator{privatestaticCachecache;static{cache=newCache();}publicstaticServicegetService(StringjndiName){Serviceservice=cache.getService(jndiName);if(service!=null){returnservice;}InitialContextcontext=newInitialContext();Serviceservice1=(Service)context.lookup(jndiName);cache.addService(service1);returnservice1;}}
6.34.7. Step 6 ¶
Use ServiceLocator To demonstrate the service locator design pattern.ServiceLocatorPatternDemo.java ¶
publicclassServiceLocatorPatternDemo{publicstaticvoidmain(String[]args){Serviceservice=ServiceLocator.getService("Service1");service.execute();service=ServiceLocator.getService("Service2");service.execute();service=ServiceLocator.getService("Service1");service.execute();service=ServiceLocator.getService("Service2");service.execute();}}
6.34.8. Step 7 ¶
Execute the program and output the result:
Looking up and creating a new Service1 object
Executing Service1
Looking up and creating a new Service2 object
Executing Service2
Returning cached Service1 object
Executing Service1
Returning cached Service2 object
Executing Service2