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

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");
   }

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

Service2.java

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

   @Override
   public 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
上一篇:基于深度学习LSTM+NLP情感分析电影数据爬虫可视化分析推荐系统(深度学习LSTM+机器学习双推荐算法+scrapy爬虫+NLP情感分析+数据分析可视化)


下一篇:PyTorch 深度学习(GPT 重译)(五)