服务定位器模式(Service Locator Pattern)用在我们想使用 JNDI 查询定位各种服务的时候。考虑到为某个服务查找 JNDI 的代价很高,服务定位器模式充分利用了缓存技术。在首次请求某个服务时,服务定位器在 JNDI 中查找服务,并缓存该服务对象。当再次请求相同的服务时,服务定位器会在它的缓存中查找,这样可以在很大程度上提高应用程序的性能。以下是这种设计模式的实体。

  • 服务(Service) - 实际处理请求的服务。对这种服务的引用可以在 JNDI 服务器中查找到。
  • Context / 初始的 Context - JNDI Context 带有对要查找的服务的引用。
  • 服务定位器(Service Locator) - 服务定位器是通过 JNDI 查找和缓存服务来获取服务的单点接触。
  • 缓存(Cache) - 缓存存储服务的引用,以便复用它们。
  • 客户端(Client) - Client 是通过 ServiceLocator 调用服务的对象。

实现

我们将创建 ServiceLocatorInitialContextCacheService 作为表示实体的各种对象。Service1Service2 表示实体服务。
ServiceLocatorPatternDemo 类在这里是作为一个客户端,将使用 ServiceLocator 来演示服务定位器设计模式
image.png

步骤 1

创建服务接口 Service。

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

步骤 2

创建实体服务

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

步骤 3

为 JNDI 查询创建 InitialContext。

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

步骤 4

创建缓存 Cache。

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

步骤 5

创建服务定位器

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

步骤 6

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

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

执行结果

  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