原文地址: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

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

  1. [Design Pattern] Service Locator Pattern 简单案例

    Service Locator Pattern,即服务定位模式,用于定位不同的服务.考虑到 InitialContext::lookup 的成本比较高,提供了 Cache 类缓存以定位到的服务. 代码 ...

  2. Service Locator Pattern 服务定位

    https://www.geeksforgeeks.org/service-locator-pattern/ Service Locator Pattern Last Updated: 06-03-2 ...

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

    本文内容 场景 目标 解决方案 实现细节 思考 相关模式 更多信息 参考资料 Common Service Locator 代码很简单,它一般不会单独使用,而是作为一个单件模式,与像 .net Uni ...

  4. 《Prism 5.0源码走读》Service Locator Pattern

    在Prism Bootstrapper里面取实例的时候使用 ServiceLocator模式,使用的是CommonServiceLocator库 (http://commonservicelocato ...

  5. [Architecture Pattern] Singleton Locator

    [Architecture Pattern] Singleton Locator 目的 组件自己提供Service Locator模式,用来降低组件的耦合度. 情景 在开发系统时,底层的Infrast ...

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

    Atitit.如何实现dip, di ,ioc  ,Service Locator的区别于联系 1. Dip原则又来自于松耦合思想方向1 2. 要实现dip原则,有以下俩个模式1 3. Ioc和di的 ...

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

    原文:https://www.dotnettricks.com/learn/dependencyinjection/understanding-inversion-of-control-depende ...

  8. Service Locator 模式

    什么是Service Locator 模式? 服务定位模式(Service Locator Pattern)是一种软件开发中的设计模式,通过应用强大的抽象层,可对涉及尝试获取一个服务的过程进行封装.该 ...

  9. [Design Patterns] 3. Software Pattern Overview

    When you're on the way which is unknown and dangerous, just follow your mind and steer the boat. 软件模 ...

随机推荐

  1. 阿里云部署java项目

    第一步:注册阿里云账号(如果有请看第二步) 1.百度搜索阿里云,点击进入阿里云官网 2.点击右上角免费注册 3.进入注册页面,按照要求填写信息 4.注册完成后登陆 登陆之后首先购买阿里云esc与服务器 ...

  2. 不安全的直接对象引用:你的 ASP.NET 应用数据是否安全?

    介绍 作为一个在X94的航空工程师,你的老板要求你从2号楼的工程图中检索出一个特定的专利.不幸的是,进入大楼需要你出示你具有进入大楼的资格的证明,然后你迅速地以徽章的形式出示给了保安.到了十三楼,进入 ...

  3. iOS开发- Images can’t contain alpha channels or transparencies 解决的方法

    新版的iTunesConnect真是各种问题阿... 蛋疼要命. 上传介绍图片(屏幕截图)的时候 , 遇到了这个问题 Images can't contain alpha channels or tr ...

  4. ftoa浮点型转换成字符串

    #include <stdio.h> bool ftos(float num,char *s,int n) {     int temp; float t=num; int pn=0; b ...

  5. ubuntu 14.04 桌面版关闭图形界面

    ubuntu 14.04 桌面版关闭图形界面 问题: 怎样将ubuntu14.04设置为文本模式启动? 解决方式: 改动改GRUB 的配置文件(不建议直接改 grub.conf) $sudo vim ...

  6. UI组件之AdapterView及其子类(四)Gallery画廊控件使用

    听说 Gallery如今已经不使用了,API使用ViewPaper取代了,以后再学专研ViewPaper吧如今说说Gallery画廊,就是不停显示图片的意思 Gallery是用来水平滚动的显示一系列项 ...

  7. 关于MAVEN找不到JDK的那点事

    自从SUN被Oracle收购以后.JDK就由Oracle来提供了. 在新版本号之中,假设你下载安装JDK以后,又选择了JRE安装(当然,如今JRE直接叫做Java了),那么,恭喜你,在 C:\wind ...

  8. 【转】android新组件RecyclerView使用介绍和进阶使用,替用Gallery

    简介: RecyclerView是support-v7包中的新组件,是一个强大的滑动组件,与经典的ListView相比,同样拥有item回收复用的功能,但是直接把viewholder的实现封装起来,用 ...

  9. 【DNN 系列】 MVC 分页

    MVC分页可采用插件形式, 有MvcPage那个插件但是我觉得那个是假分页 有点影响效率 所以网上找了一个例子来 做分页 1, PagerQuery.cs public class PagerQuer ...

  10. Generic type test java

    package test; public class GenericTest { public class Room<T> { private T t; public void add(T ...