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

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

实现

我们将创建 ServiceLocatorInitialContextCacheService 作为表示实体的各种对象。Service1 和 Service2 表示实体服务。

ServiceLocatorPatternDemo,我们的演示类在这里是作为一个客户端,将使用 ServiceLocator 来演示服务定位器设计模式。

步骤 1

创建服务接口 Service。

Service.java

package gof.servicelocatorpattern;

public interface Service {

    String getName();
void execute();
}

步骤 2

创建实体服务。

Service1.java

package gof.servicelocatorpattern;

public class Service1 implements Service{

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

Service2.java

package gof.servicelocatorpattern;

public class Service2 implements Service{

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

步骤 3

为 JNDI 查询创建 InitialContext。

InitialContext.java

package gof.servicelocatorpattern;

public class InitialContext {

    public Object lookup(String jndiName){
if("SERVICE1".equalsIgnoreCase(jndiName)){
System.out.println("Looking up and Creating a new Service1 object");
return new Service1();
}else if("SERVICE2".equalsIgnoreCase(jndiName)){
System.out.println("Looking up and Creating a new Service2 object");
return new Service2();
}
return null;
}
}

步骤 4

创建缓存 Cache。

Cache.java

package gof.servicelocatorpattern;

import java.util.ArrayList;
import java.util.List; public class Cache { private List<Service> services; public Cache() {
super();
services = new ArrayList<Service>();
} public Service getServices(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 service){
boolean exists = false;
for(Service ser : services){
if(ser.getName().equalsIgnoreCase(service.getName())){
exists = true;
}
}
if(!exists){
services.add(service);
}
}
}

步骤 5

创建服务定位器。

ServiceLocator.java

package gof.servicelocatorpattern;

public class ServiceLocator {

    private static Cache cache;
static {
cache = new Cache();
}
public static Service getService(String jndiName){
Service service = cache.getServices(jndiName);
if(null != service){
return service;
}
InitialContext context = new InitialContext();
service = (Service) context.lookup(jndiName);
cache.addService(service);
return service;
}
}

步骤 6

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

ServiceLocatorPatternDemo.java

package gof.servicelocatorpattern;

public class ServiceLocatorPatternDemo {

    public static void main(String[] args) {
Service service = null;
service = ServiceLocator.getService("Service1");
service.execute();
service = ServiceLocator.getService("Service2");
service.execute();
service = ServiceLocator.getService("Service1");
service.execute();
service = ServiceLocator.getService("Service2");
service.execute();
}
}

步骤 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

啦啦啦

最新文章

  1. Strus2学习:基础(一)
  2. iOS socket保持后台连接 ios9.0 xcode8.0
  3. Postgresql存储过程调试:PostgreSQL 之 Function NOTICE
  4. Kubuntu 使用YaH3C进行中大校园网认证
  5. locate无法open mlocate.db
  6. Amazon的Fire Phone之于Android开发者
  7. 【iOS】Quartz2D图片剪切
  8. Docker系列(二)组件介绍
  9. xml技术基础
  10. 把EXCEL列号数字变成字母
  11. 增加配置Apache2 管理 SVN 访问权限
  12. SAXParserFactory
  13. 支持向量机(Support Vector Machine):超平面
  14. 反射Dll注入分析
  15. linux为什么要使用CentOS开发?
  16. Linux及MacOSX中使用zsh
  17. Java 16进制转10进制
  18. String和int互相转换,String转float
  19. C# 获取物理网卡Mac地址
  20. codeforces #296 div2 (527C) STL中set的运用

热门文章

  1. 【linux】——ubuntu12.04 下安装wine和wine乱码解决方案
  2. CLR、内存分配和垃圾回收
  3. C程序模拟实现银行家算法
  4. Jquery函数大全 - 案例说明
  5. bom头解释方法和去掉方法
  6. modelsim 出现此错误怎么办
  7. 第三百四十八节,Python分布式爬虫打造搜索引擎Scrapy精讲—通过自定义中间件全局随机更换代理IP
  8. JUnit套件测试实例
  9. rrnDB数据库简介-16S基因多拷贝数的证据
  10. 每天一个linux命令:cd命令