java设计模式之服务定位器模式

博客介绍了服务定位器模式,该模式用于通过JNDI查询定位服务,利用缓存技术提高应用程序性能。首次请求服务时在JNDI查找并缓存,再次请求时直接从缓存获取。还介绍了该模式的实体,如服务、Context、服务定位器等,并说明了实现步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

服务定位器模式(Service Locator Pattern)用在我们想使用JNDI查询定位各种服务的时候。考虑到为某个服务查找JNDI的代价很高,服务定位器模式充分利用了缓存技术。在首次请求某个服务时,服务定位器在JNDI中查找服务,并缓存该服务对象。当再次请求相同的服务时,服务定位器会在它的缓存中查找,这样可以很大程度上提高应用程序的性能。以下时这种设计模式的实体。

  • 服务(Service)-实际处理请求的服务。对这种服务的引用可以在JNDI服务器中查找到。
  • Context/初始的Context-JNDI Context带有对要查找的服务的引用。
  • 服务定位器(Service Locator)-服务定位器是通过JNDI查找和缓存服务来获取服务的单点接触。
  • 缓存(Cache)-缓存存储服务的引用,以便复用它们。
  • 客户端(Client)-Client是通过ServiceLocator调用服务的对象。

实现:

创建ServiceLocator、InitialContext、Cache、Service作为表示实体的各种对象。Service1和Service2表示实体服务。

创建服务接口Service:

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

创建实体服务:

//创建实体服务
public class Service1 implents Service{
    public void execute(){
        System.out.println("Executing Service1");
    }
    
    @Override
    public String getName() {
        return "Service1";
    }
}
//实体服务
public class Service2 implements Service {
   public void execute(){
      System.out.println("Executing Service2");
   }
 
   @Override
   public String getName() {
      return "Service2";
   }
}

为JNDI查询创建InitialContext:

public class InitialContext{
    pulblic 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;
    }
}

创建缓存Cache:

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 addService(Service newService){
        boolean exists = false;
        for(Service service : services){
            if(service.getName().equalsIgnoreCase(newService.getName())){
                exists = true;
            }
        }
        if(!exists){
            services.add(newService);
        }
    }
}

创建服务定位器:

//服务定位器
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 servica1 = (Service)context.lookup(jndiName);
        cache.addService(service1);
        return service1;
    }
}

演示服务定位器模式:

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

执行程序,输出结果:

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值