橘子味的心
标题:Java服务定位器模式

当我们想要使用JNDI查找来定位各种服务时,使用服务定位器设计模式。 考虑到为服务查找JNDI的高成本,所以在服务定位器模式使用缓存技术。 首次需要服务时,服务定位器在JNDI中查找并缓存服务对象。 通过服务定位器进一步查找或相同的服务在其缓存中完成,这在很大程度上提高了应用的性能。 以下是这种类型的设计模式的实体。

  • 服务 - 将处理请求的实际服务。 将在JNDI服务器中查找此类服务的引用。
  • 上下文/初始上下文 - JNDI上下文携带对用于查找目的的服务的引用。
  • 服务定位器 - 服务定位器是通过JNDI查找缓存服务获得服务的单一联系点。
  • 缓存 - 用于存储服务的引用以重用它们的缓存。
  • 客户端 - 客户端是通过ServiceLocator调用服务的对象。

实现实例

在这个实现的实例中,将创建一个ServiceLocatorInitialContextCacheService作为表示实体的各种对象。 Service1Service2用来表示具体服务。

ServiceLocatorPatternDemo是一个演示类,在这里充当客户端,将使用ServiceLocator演示服务定位器设计模式。

服务定位器模式示例的结构如下图所示 -

第1步

创建一个名为 Service 的接口,其代码如下 -
Service.java

  1. public interface Service {
  2. public String getName();
  3. public void execute();
  4. }
  5. Java

第2步

创建具体的服务类,其代码如下 -

Service1.java

  1. public class Service1 implements Service {
  2. public void execute(){
  3. System.out.println("Executing Service1");
  4. }
  5.  
  6. @Override
  7. public String getName() {
  8. return "Service1";
  9. }
  10. }
  11. Java

Service2.java

  1. public class Service2 implements Service {
  2. public void execute(){
  3. System.out.println("Executing Service2");
  4. }
  5.  
  6. @Override
  7. public String getName() {
  8. return "Service2";
  9. }
  10. }
  11. Java

第3步

JNDI查找创建InitialContext 类,其代码如下 -
InitialContext.java

  1. public class InitialContext {
  2. public Object lookup(String jndiName){
  3.  
  4. if(jndiName.equalsIgnoreCase("SERVICE1")){
  5. System.out.println("Looking up and creating a new Service1 object");
  6. return new Service1();
  7. }
  8. else if (jndiName.equalsIgnoreCase("SERVICE2")){
  9. System.out.println("Looking up and creating a new Service2 object");
  10. return new Service2();
  11. }
  12. return null;
  13. }
  14. }
  15. Java

第4步

创建缓存类,其代码如下 -
Cache.java

  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. public class Cache {
  5.  
  6. private List<Service> services;
  7.  
  8. public Cache(){
  9. services = new ArrayList<Service>();
  10. }
  11.  
  12. public Service getService(String serviceName){
  13.  
  14. for (Service service : services) {
  15. if(service.getName().equalsIgnoreCase(serviceName)){
  16. System.out.println("Returning cached " + serviceName + " object");
  17. return service;
  18. }
  19. }
  20. return null;
  21. }
  22.  
  23. public void addService(Service newService){
  24. boolean exists = false;
  25.  
  26. for (Service service : services) {
  27. if(service.getName().equalsIgnoreCase(newService.getName())){
  28. exists = true;
  29. }
  30. }
  31. if(!exists){
  32. services.add(newService);
  33. }
  34. }
  35. }
  36. Java

第5步

创建服务定位器,其代码如下 -

ServiceLocator.java

  1. public class ServiceLocator {
  2. private static Cache cache;
  3.  
  4. static {
  5. cache = new Cache();
  6. }
  7.  
  8. public static Service getService(String jndiName){
  9.  
  10. Service service = cache.getService(jndiName);
  11.  
  12. if(service != null){
  13. return service;
  14. }
  15.  
  16. InitialContext context = new InitialContext();
  17. Service service1 = (Service)context.lookup(jndiName);
  18. cache.addService(service1);
  19. return service1;
  20. }
  21. }
  22. Java

第6步

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

  1. public class ServiceLocatorPatternDemo {
  2. public static void main(String[] args) {
  3. Service service = ServiceLocator.getService("Service1");
  4. service.execute();
  5. service = ServiceLocator.getService("Service2");
  6. service.execute();
  7. service = ServiceLocator.getService("Service1");
  8. service.execute();
  9. service = ServiceLocator.getService("Service2");
  10. service.execute();
  11. }
  12. }
  13. Java

第7步

验证输出,执行上面的代码得到以下结果 -

  1. Looking up and creating a new Service1 object
  2. Executing Service1
  3. Looking up and creating a new Service2 object
  4. Executing Service2
  5. Returning cached Service1 object
  6. Executing Service1
  7. Returning cached Service2 object
  8. Executing Service2
  9. Java