- 设计模式教程
- 设计模式 - 主页
- 设计模式 - 概述
- 设计模式-工厂模式
- 抽象工厂模式
- 设计模式-单例模式
- 设计模式-构建器模式
- 设计模式 - 原型模式
- 设计模式-适配器模式
- 设计模式-桥接模式
- 设计模式-过滤器模式
- 设计模式-复合模式
- 设计模式 - 装饰模式
- 设计模式-外观模式
- 设计模式-享元模式
- 设计模式-代理模式
- 责任链模式
- 设计模式-命令模式
- 设计模式-解释器模式
- 设计模式-迭代器模式
- 设计模式——中介者模式
- 设计模式 - 纪念品模式
- 设计模式-观察者模式
- 设计模式-状态模式
- 设计模式-空对象模式
- 设计模式-策略模式
- 设计模式-模板模式
- 设计模式-访客模式
- 设计模式-MVC模式
- 业务代表模式
- 复合实体模式
- 数据访问对象模式
- 前控制器模式
- 拦截过滤器模式
- 服务定位器模式
- 传输对象模式
设计模式-服务定位器模式
当我们想要使用 JNDI 查找来定位各种服务时,就会使用服务定位器设计模式。考虑到查找服务的 JNDI 成本较高,服务定位器模式利用了缓存技术。第一次需要服务时,服务定位器在 JNDI 中查找并缓存服务对象。通过服务定位器进一步查找或相同服务是在其缓存中完成的,这在很大程度上提高了应用程序的性能。以下是此类设计模式的实体。
服务- 将处理请求的实际服务。此类服务的引用可在 JNDI 服务器中查看。
上下文/初始上下文- JNDI 上下文携带对用于查找目的的服务的引用。
服务定位器- 服务定位器是通过 JNDI 查找缓存服务来获取服务的单点联系。
缓存- 用于存储服务引用以重用它们的缓存
客户端- 客户端是通过 ServiceLocator 调用服务的对象。
执行
我们将创建一个ServiceLocator、InitialContext、Cache、Service作为代表我们实体的各种对象。Service1和Service2代表具体服务。
ServiceLocatorPatternDemo是我们的演示类,在这里充当客户端,并将使用ServiceLocator来演示服务定位器设计模式。
步骤1
创建服务接口。
服务.java
public interface Service {
public String getName();
public void execute();
}
第2步
创建具体服务。
服务1.java
public class Service1 implements Service {
public void execute(){
System.out.println("Executing Service1");
}
@Override
public String getName() {
return "Service1";
}
}
服务2.java
public class Service2 implements Service {
public void execute(){
System.out.println("Executing Service2");
}
@Override
public String getName() {
return "Service2";
}
}
步骤3
为 JNDI 查找创建 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;
}
}
步骤4
创建缓存
缓存.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);
}
}
}
步骤5
创建服务定位器
服务定位器.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;
}
}
步骤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();
}
}
步骤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