Design Pattern - Service Locator Pattern--转载

原文地址:http://www.tutorialspoint.com/design_pattern/service_locator_pattern.htm

The service locator design pattern is used when we want to locate various services using JNDI lookup. Considering high cost of looking up JNDI for a service, Service Locator pattern makes use of caching technique. For the first time a service is required, Service Locator looks up in JNDI and caches the service object. Further lookup or same service via Service Locator is done in its cache which improves the performance of application to great extent. Following are the entities of this type of design pattern.

  • Service - Actual Service which will process the request. Reference of such service is to be looked upon in JNDI server.
  • Context / Initial Context - JNDI Context carries the reference to service used for lookup purpose.
  • Service Locator - Service Locator is a single point of contact to get services by JNDI lookup caching the services.
  • Cache - Cache to store references of services to reuse them
  • Client - Client is the object that invokes the services via ServiceLocator.

Implementation

We are going to create a ServiceLocator,InitialContextCacheService as various objects representing our entities.Service1 and Service2 represent concrete services.

ServiceLocatorPatternDemo, our demo class, is acting as a client here and will useServiceLocator to demonstrate Service Locator Design Pattern.

Step 1

Create Service interface.

Service.java

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

Step 2

Create concrete services.

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

Step 3

Create InitialContext for JNDI lookup

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

Step 4

Create Cache

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

Step 5

Create Service Locator

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

Step 6

Use the ServiceLocator to demonstrate Service Locator Design Pattern.

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

Step 7

Verify the output.

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
时间: 2024-10-10 09:09:52

Design Pattern - Service Locator Pattern--转载的相关文章

[Design Pattern] Service Locator Pattern 简单案例

Service Locator Pattern,即服务定位模式,用于定位不同的服务.考虑到 InitialContext::lookup 的成本比较高,提供了 Cache 类缓存以定位到的服务. 代码实现 Service 接口 public interface Service { public String getName(); public void execute(); } Service1, Service2 实现 Service 接口,提供具体服务 public class Servic

.NET 服务器定位模式(Service Locator Pattern)

本文内容 场景 目标 解决方案 实现细节 思考 相关模式 更多信息 参考资料 下载 Demo 场景 你有一个类,该类依赖几个服务 Service(事实上,这些服务可以看做是另几个类),这些服务是在编译阶段指定具体类型的.在接下来的例子中,ClassA 在编译阶段依赖 ServiceA 和ServiceB.下图说明这个问题. 这种情况有如下缺点: 若替换或更新依赖的服务(或类),显然,必须修改源代码,并且重新编译解决方案: 这些依赖的具体实现(依赖的服务的具体实现)必须在编译时可用:The con

[Architecture Pattern] Singleton Locator

[Architecture Pattern] Singleton Locator 目的 组件自己提供Service Locator模式,用来降低组件的耦合度. 情景 在开发系统时,底层的Infrastructure Context.或是核心的Domain Context这些共享对象生成之后,会在系统的许多地方被使用.为了减少共享对象初始生成.参考传递所造成的困扰,可以在系统内套用Service Locator模式,提供统一的静态参考点来生成.存取这些共享对象. Service Locator参考

Atitit。如何实现dip, di ,ioc ,Service Locator的区别于联系

Atitit.如何实现dip, di ,ioc  ,Service Locator的区别于联系 1. Dip原则又来自于松耦合思想方向1 2. 要实现dip原则,有以下俩个模式1 3. Ioc和di的区别1 4. Service Locator模式1 5.  Service Locator vs. Dependency Injection2 6. 参考2 1. Dip原则又来自于松耦合思想方向 松耦合系统的好处有两点:一点是它适应变化的灵活性:另一点是当某个服务的内部结构和实现逐渐发生改变时,不

【转】Understanding Inversion of Control, Dependency Injection and Service Locator Print

原文:https://www.dotnettricks.com/learn/dependencyinjection/understanding-inversion-of-control-dependency-injection-and-service-locator ----------------------------------------------------------------------------------------- Understanding Inversion of

Service Locator 模式

什么是Service Locator 模式? 服务定位模式(Service Locator Pattern)是一种软件开发中的设计模式,通过应用强大的抽象层,可对涉及尝试获取一个服务的过程进行封装.该模式使用一个称为"Service Locator"的中心注册表来处理请求并返回处理特定任务所需的必要信息. 场景描述 某类ClassA依赖于服务ServiceA和服务ServiceB,服务的具体类型需在编译时指定. 这种条件下有以下缺点: 尝试替换或更新依赖项,必须更改类的源代码并且重新编

Learning JavaScript Design Patterns The Singleton Pattern

The Singleton Pattern The Singleton pattern is thus known because it restricts instantiation of a class to a single object. Classically, the Singleton pattern can be implemented by creating a class with a method that creates a new instance of the cla

[Design Patterns] 4. Creation Pattern

设计模式是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结,使用设计模式的目的是提高代码的可重用性,让代码更容易被他人理解,并保证代码可靠性.它是代码编制真正实现工程化. 四个关键元素:(1) Pattern Name, (2) Problem, (3) Solution, (4) Consequences. 01. Factory Method Pattern /* The product should be created by his own factory. */ Log

Learning JavaScript Design Patterns The Module Pattern

The Module Pattern Modules Modules are an integral piece of any robust application's architecture and typically help in keeping the units of code for a project both cleanly separated and organized. In JavaScript, there are several options for impleme