鍍金池/ 教程/ Java/ Java服務(wù)定位器模式
Java前端控制器模式
Java工廠設(shè)計(jì)模式
Java抽象工廠模式
Java觀察者模式
Java門面模式(或外觀模式)
Java備忘錄模式
Java MVC模式
Java單例模式
Java傳輸對象模式
Java迭代器模式
Java責(zé)任鏈模式
Java命令模式
Java原型模式
Java解釋器模式
Java適配器模式
Java狀態(tài)模式
Java中介者模式(Mediator Pattern)
Java攔截過濾器模式
Java策略模式
Java組合模式
Java業(yè)務(wù)代理模式
Java裝飾模式
Java模板模式
Java橋接模式
Java過濾器模式(條件模式)
Java享元模式(Flyweight Pattern)
Java建造者(Builder)模式
Java設(shè)計(jì)模式
Java空對象模式
Java數(shù)據(jù)訪問對象模式
Java訪問者模式
Java組合實(shí)體模式
Java服務(wù)定位器模式

Java服務(wù)定位器模式

當(dāng)我們想要使用JNDI查找來定位各種服務(wù)時(shí),使用服務(wù)定位器設(shè)計(jì)模式。 考慮到為服務(wù)查找JNDI的高成本,所以在服務(wù)定位器模式使用緩存技術(shù)。 首次需要服務(wù)時(shí),服務(wù)定位器在JNDI中查找并緩存服務(wù)對象。 通過服務(wù)定位器進(jìn)一步查找或相同的服務(wù)在其緩存中完成,這在很大程度上提高了應(yīng)用的性能。 以下是這種類型的設(shè)計(jì)模式的實(shí)體。

  • 服務(wù) - 將處理請求的實(shí)際服務(wù)。 將在JNDI服務(wù)器中查找此類服務(wù)的引用。
  • 上下文/初始上下文 - JNDI上下文攜帶對用于查找目的的服務(wù)的引用。
  • 服務(wù)定位器 - 服務(wù)定位器是通過JNDI查找緩存服務(wù)獲得服務(wù)的單一聯(lián)系點(diǎn)。
  • 緩存 - 用于存儲(chǔ)服務(wù)的引用以重用它們的緩存。
  • 客戶端 - 客戶端是通過ServiceLocator調(diào)用服務(wù)的對象。

實(shí)現(xiàn)實(shí)例

在這個(gè)實(shí)現(xiàn)的實(shí)例中,將創(chuàng)建一個(gè)ServiceLocator,InitialContext,Cache,Service作為表示實(shí)體的各種對象。 Service1Service2用來表示具體服務(wù)。

ServiceLocatorPatternDemo是一個(gè)演示類,在這里充當(dāng)客戶端,將使用ServiceLocator演示服務(wù)定位器設(shè)計(jì)模式。

服務(wù)定位器模式示例的結(jié)構(gòu)如下圖所示 -

第1步

創(chuàng)建一個(gè)名為 Service 的接口,其代碼如下 -
Service.java

public interface Service {
   public String getName();
   public void execute();
}

第2步

創(chuàng)建具體的服務(wù)類,其代碼如下 -

Service1.java

public class Service1 implements Service {
   public void execute(){
      System.out.println("Executing Service1");
   }

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

Service2.java

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

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

第3步

JNDI查找創(chuàng)建InitialContext 類,其代碼如下 -
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步

創(chuàng)建緩存類,其代碼如下 -
Cache.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步

創(chuàng)建服務(wù)定位器,其代碼如下 -

ServiceLocator.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類來演示服務(wù)定位器設(shè)計(jì)模式。
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步

驗(yàn)證輸出,執(zhí)行上面的代碼得到以下結(jié)果 -

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