当前位置: 首页 > news >正文

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

Java服务定位器模式

    • 实现
    • 范例

  • 服务定位器模式(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 表示实体服务
  • ServiceLocatorPatternDemo ,我们的演示类在这里是作为一个客户端,将使用 ServiceLocator 来演示服务定位器设计模式

范例

1. 创建服务接口 Service

Service.java

package com.demo.gof;
public interface Service{public String getName();public void execute();
}

2. 创建实体服务

Service1.java

package com.demo.gof;
public class Service1 implements Service {public void execute(){System.out.println("Executing Service1");}@Overridepublic String getName() {return "Service1";}
}

Service2.java

package com.demo.gof;
public class Service2 implements Service {public void execute(){System.out.println("Executing Service2");}@Overridepublic String getName() {return "Service2";}
}

3. 为 JNDI 查询创建 InitialContext

InitialContext.java

package com.demo.gof;
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;      }
}

4. 创建缓存 Cache

Cache.java

package com.demo.gof;
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);}}
}

5. 创建服务定位器 ServiceLocator

ServiceLocator.java

package com.demo.gof;
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;}
}

6. 使用 ServiceLocator 来演示服务定位器设计模式

ServiceLocatorPatternDemo.java

package com.demo.gof;
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 范例,输出结果如下```java
$ javac -d . src/main/com.demo/gof/ServiceLocatorPatternDemo.java
$ java  com.demo.gof.ServiceLocatorPatternDemo
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
http://www.lryc.cn/news/324495.html

相关文章:

  • 前端小卡片:vue3路由是什么,有什么作用,该如何配置?
  • Jackson 2.x 系列【2】生成器 JsonGenerator
  • 说说webpack中常见的Loader?解决了什么问题?
  • Django 铺垫
  • 浅谈C++的继承与多态(静态绑定、动态绑定和虚函数等)
  • 【无人机综合考试题】
  • JS精度计算的几种解决方法,1、转换成整数计算后再转换成小数,2、toFixed,3、math.js,4、bignumber.js,5、big.js
  • v77.递归
  • Spring Cloud微服务功能及其组件详细讲解
  • (三维重建学习)已有位姿放入colmap和3D Gaussian Splatting训练
  • 4635: 【搜索】【广度优先】回家
  • Uibot6.0 (RPA财务机器人师资培训第1天 )RPA+AI、RPA基础语法
  • 【吊打面试官系列】Redis篇 -Redis集群的主从复制模型是怎样的?
  • 高效的二进制列化格式 MessagePack 详解
  • 鸿蒙Harmony应用开发—ArkTS-if/else:条件渲染
  • JAVA 100道题(14)
  • STM32+ESP8266水墨屏天气时钟:简易多级菜单(数组查表法)
  • 数学建模综合评价模型与决策方法
  • window下安装并使用nvm(含卸载node、卸载nvm、全局安装npm)
  • Mysql——基础命令集合
  • 记录一次流相关故障
  • linux源配置:ubuntu、centos;lspci与lsmod命令区别
  • 面试算法-88-反转链表
  • 如何在个人Windows电脑搭建Cloudreve云盘并实现无公网IP远程访问
  • 一文详解Rust中的字符串
  • Mysql中用户密码修改
  • day14-SpringBoot 原理篇
  • ChatGPT论文指南|揭秘8大ChatGPT提示词研究技巧提升写作效率【建议收藏】
  • P1563 [NOIP2016 提高组] 玩具谜题
  • 【数据库】数据库语言