Java Service Locator Pattern(服務器定位模式)

服務定位器模式(Service Locator Pattern)用在我們想使用 JNDI 查詢定位各種服務的時候??紤]到為某個服務查找 JNDI 的代價很高,服務定位器模式充分利用了緩存技術。在首次請求某個服務時,服務定位器在 JNDI 中查找服務,并緩存該服務對象。當再次請求相同的服務時,服務定位器會在它的緩存中查找,這樣可以在很大程度上提高應用程序的性能。以下是這種設計模式的實體。

  • 服務(Service) - 實際處理請求的服務。對這種服務的引用可以在 JNDI 服務器中查找到。
    Context / 初始的 Context - JNDI Context 帶有對要查找的服務的引用。
  • 服務定位器(Service Locator) - 服務定位器是通過 JNDI 查找和緩存服務來獲取服務的單點接觸。
  • 緩存(Cache) - 緩存存儲服務的引用,以便復用它們。
  • 客戶端(Client) - Client 是通過 ServiceLocator 調用服務的對象。
  1. 創建服務接口 Service。
/**
 * 1. 創建服務接口 Service。
 * @author mazaiting
 */
public interface Service {
    public String getName();
    public void execute();
}
  1. 創建實體服務。
/**
 * 2. 創建實體服務。
 * @author mazaiting
 */
public class Service1 implements Service{

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

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

}

/**
 * 2. 創建實體服務。
 * @author mazaiting
 */
public class Service2 implements Service{

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

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

}
  1. 為 JNDI 查詢創建 InitialContext。
/**
 * 3. 為 JNDI 查詢創建 InitialContext。
 * @author mazaiting
 */
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;
    }
}
  1. 創建緩存 Cache。
/**
 * 4. 創建緩存 Cache。
 * @author mazaiting
 */
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  "+service+" 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);
        }
    }
    
}
  1. 創建服務定位器。
/**
 * 5. 創建服務定位器。
 * @author mazaiting
 */
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;        
    }
    
}
  1. 使用 ServiceLocator 來演示服務定位器設計模式。
/**
 * 6. 使用 ServiceLocator 來演示服務定位器設計模式。
 * @author mazaiting
 */
public class Client {
    
    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();      
    }
    
}
  1. 打印結果
Looking up and creating a new Service1 object
Executing Service1
Looking up and creating a new Service2 object
Executing Service2
Returning cached  com.mazaiting.serloc.Service1@15db9742 object
Executing Service1
Returning cached  com.mazaiting.serloc.Service2@6d06d69c object
Executing Service2
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,767評論 18 399
  • Spring Boot 參考指南 介紹 轉載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,970評論 6 342
  • 一. Java基礎部分.................................................
    wy_sure閱讀 3,838評論 0 11
  • (1)《秋感》 落木凋陰花亦瘦,晚夕愈早只孤舟。 水暗山行云皺月,金風季望探高樓。 古來皆嘆寒秋...
    林若玄葉閱讀 368評論 0 26