Java服务定位器模式

Java服务定位器模式


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


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


在这个实现的实例中,将创建一个ServiceLocator,InitialContext,Cache,Service作为表示实体的各种对象。 Service1和Service2用来表示具体服务。


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 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值