日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Design Pattern - Service Locator Pattern--转载

發布時間:2025/4/5 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Design Pattern - Service Locator Pattern--转载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文地址:http://www.tutorialspoint.com/design_pattern/service_locator_pattern.htm

The service locator design pattern is used when we want to locate various services using JNDI lookup. Considering high cost of looking up JNDI for a service, Service Locator pattern makes use of caching technique. For the first time a service is required, Service Locator looks up in JNDI and caches the service object. Further lookup or same service via Service Locator is done in its cache which improves the performance of application to great extent. Following are the entities of this type 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 ServiceLocator.

Implementation

We are going to create a?ServiceLocator,InitialContext,?Cache,?Service?as various objects representing our entities.Service1?and?Service2?represent concrete services.

ServiceLocatorPatternDemo, our demo class, is acting as a client here and will useServiceLocator?to demonstrate Service Locator Design Pattern.

Step 1

Create Service interface.

Service.java

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

Step 2

Create concrete services.

Service1.java

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

Service2.java

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

Step 3

Create InitialContext for JNDI lookup

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; } }

Step 4

Create Cache

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); } } }

Step 5

Create Service Locator

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; } }

Step 6

Use the?ServiceLocator?to demonstrate Service Locator Design Pattern.

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(); } }

Step 7

Verify the output.

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

?

轉載于:https://www.cnblogs.com/davidwang456/p/4455841.html

總結

以上是生活随笔為你收集整理的Design Pattern - Service Locator Pattern--转载的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。