Java服务定位器模式

来源:互联网 发布:曼彻斯特城市大学 知乎 编辑:程序博客网 时间:2024/05/23 23:36

当我们想要使用JNDI查找来定位各种服务时,使用服务定位器设计模式。 考虑到为服务查找JNDI的高成本,所以在服务定位器模式使用缓存技术。 首次需要服务时,服务定位器在JNDI中查找并缓存服务对象。 通过服务定位器进一步查找或相同的服务在其缓存中完成,这在很大程度上提高了应用的性能。 以下是这种类型的设计模式的实体。

  • 服务 - 将处理请求的实际服务。 将在JNDI服务器中查找此类服务的引用。
  • 上下文/初始上下文 - JNDI上下文携带对用于查找目的的服务的引用。
  • 服务定位器 - 服务定位器是通过JNDI查找缓存服务获得服务的单一联系点。
  • 缓存 - 用于存储服务的引用以重用它们的缓存。
  • 客户端 - 客户端是通过ServiceLocator调用服务的对象。

实现实例

在这个实现的实例中,将创建一个ServiceLocatorInitialContextCacheService作为表示实体的各种对象。Service1Service2用来表示具体服务。

ServiceLocatorPatternDemo是一个演示类,在这里充当客户端,将使用ServiceLocator演示服务定位器设计模式。

服务定位器模式示例的结构如下图所示 -

第1步

创建一个名为 Service 的接口,其代码如下 -
Service.java

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

第2步

创建具体的服务类,其代码如下 -

Service1.java

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

Service2.java

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

第3步

JNDI查找创建InitialContext 类,其代码如下 -
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;           }}
Java

第4步

创建缓存类,其代码如下 -
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);      }   }}
Java

第5步

创建服务定位器,其代码如下 -

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

第6步

使用ServiceLocator类来演示服务定位器设计模式。
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();           }}
Java

第7步

验证输出,执行上面的代码得到以下结果 -

Looking up and creating a new Service1 objectExecuting Service1Looking up and creating a new Service2 objectExecuting Service2Returning cached  Service1 objectExecuting Service1Returning cached  Service2 objectExecuting Service2