Java Service Locator Pattern(服务器定位模式)

简介: 服务定位器模式(Service Locator Pattern)用在我们想使用 JNDI 查询定位各种服务的时候。考虑到为某个服务查找 JNDI 的代价很高,服务定位器模式充分利用了缓存技术。

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

  • 服务(Service) - 实际处理请求的服务。对这种服务的引用可以在 JNDI 服务器中查找到。
    Context / 初始的 Context - JNDI Context 带有对要查找的服务的引用。
  • 服务定位器(Service Locator) - 服务定位器是通过 JNDI 查找和缓存服务来获取服务的单点接触。
  • 缓存(Cache) - 缓存存储服务的引用,以便复用它们。
  • 客户端(Client) - Client 是通过 ServiceLocator 调用服务的对象。
  1. 创建服务接口 Service。
/**
 * 1. 创建服务接口 Service。
 * @author mazaiting
 */
public interface Service {
    public String getName();
    public void execute();
}
  1. 创建实体服务。
/**
 * 2. 创建实体服务。
 * @author mazaiting
 */
public class Service1 implements Service{

    public String getName() {
        return "Service1";
    }

    public void execute() {
        System.out.println("Executing Service1");       
    }

}

/**
 * 2. 创建实体服务。
 * @author mazaiting
 */
public class Service2 implements Service{

    public String getName() {
        return "Service2";
    }

    public void execute() {
        System.out.println("Executing Service2");       
    }

}
  1. 为 JNDI 查询创建 InitialContext。
/**
 * 3. 为 JNDI 查询创建 InitialContext。
 * @author mazaiting
 */
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;
    }
}
  1. 创建缓存 Cache。
/**
 * 4. 创建缓存 Cache。
 * @author mazaiting
 */
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  "+service+" 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);
        }
    }
    
}
  1. 创建服务定位器。
/**
 * 5. 创建服务定位器。
 * @author mazaiting
 */
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;        
    }
    
}
  1. 使用 ServiceLocator 来演示服务定位器设计模式。
/**
 * 6. 使用 ServiceLocator 来演示服务定位器设计模式。
 * @author mazaiting
 */
public class Client {
    
    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();      
    }
    
}
  1. 打印结果
Looking up and creating a new Service1 object
Executing Service1
Looking up and creating a new Service2 object
Executing Service2
Returning cached  com.mazaiting.serloc.Service1@15db9742 object
Executing Service1
Returning cached  com.mazaiting.serloc.Service2@6d06d69c object
Executing Service2
目录
相关文章
|
5天前
|
网络协议 Dubbo Java
【网络编程】理解客户端和服务器并使用Java提供的api实现回显服务器
【网络编程】理解客户端和服务器并使用Java提供的api实现回显服务器
10 0
|
6天前
|
关系型数据库 MySQL Java
通过使用阿里云服务器,搭建Java程序的运行环境
通过使用阿里云服务器,搭建Java程序的运行环境
|
7天前
|
网络协议 Ubuntu Java
如何使用MCSM搭建我的世界Java版服务器并实现远程联机游戏
如何使用MCSM搭建我的世界Java版服务器并实现远程联机游戏
23 0
|
7天前
|
传感器 人工智能 前端开发
JAVA语言VUE2+Spring boot+MySQL开发的智慧校园系统源码(电子班牌可人脸识别)Saas 模式
智慧校园电子班牌,坐落于班级的门口,适合于各类型学校的场景应用,班级学校日常内容更新可由班级自行管理,也可由学校统一管理。让我们一起看看,电子班牌有哪些功能呢?
98 4
JAVA语言VUE2+Spring boot+MySQL开发的智慧校园系统源码(电子班牌可人脸识别)Saas 模式
|
12天前
|
设计模式 前端开发 Java
19:Web开发模式与MVC设计模式-Java Web
19:Web开发模式与MVC设计模式-Java Web
22 4
|
12天前
|
Java
如何解决使用若依前后端分离打包部署到服务器上后主包无法找到从包中的文件的问题?如何在 Java 代码中访问 jar 包中的资源文件?
如何解决使用若依前后端分离打包部署到服务器上后主包无法找到从包中的文件的问题?如何在 Java 代码中访问 jar 包中的资源文件?
50 0
|
4天前
|
弹性计算 运维 监控
解密阿里云弹性计算:探索云服务器ECS的核心功能
阿里云ECS是核心计算服务,提供弹性云服务器资源,支持实例按需配置、集群管理和监控,集成安全防护,确保服务稳定、安全,助力高效业务运营。
30 0
|
1天前
|
弹性计算 运维 安全
阿里云ecs使用体验
整了台服务器部署项目上线
|
4天前
|
负载均衡 固态存储 Linux
阿里云轻量应用服务器、云服务器、gpu云服务器最新收费标准参考
轻量应用服务器、云服务器、gpu云服务器是阿里云服务器产品中,比较热门的云服务器产品类型,不同类型的云服务器产品收费模式与收费标准是不一样的,本文为大家展示这几个云服务器产品的最新收费标准情况,以供参考。
阿里云轻量应用服务器、云服务器、gpu云服务器最新收费标准参考
|
4天前
|
弹性计算 负载均衡 容灾
应用阿里云弹性计算:打造高可用性云服务器ECS架构
阿里云弹性计算助力构建高可用云服务器ECS架构,通过实例分布、负载均衡、弹性IP、数据备份及多可用区部署,确保业务连续稳定。自动容错和迁移功能进一步增强容灾能力,提供全方位高可用保障。
16 0