原文地址: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. nginx开启gzip压缩后导致apk包下载不能正常安装

    最后更新时间:2019/4/27 nginx一般都会开启gzip压缩,以提升传输性能. 配置如下: gzip on; gzip_comp_level 2; gzip_min_length 1k; gz ...

  2. 紫书 例题8-8 UVa 1471 (用set实现动态二分)

    设切割的区间为(j, i), 注意两边都是开区间. 然后可以预处理出以i为起点的最长连续递增的长度和以j为终点的最长连续递增的长度. 大致思路就是枚举i,右边这一侧的最优值就知道了, 然后这道题的关键 ...

  3. 紫书 习题 8-1 UVa 1149(贪心)

    排序之后, 尽量最小和最大的放在一个背包, 放不下就放最大的. #include<cstdio> #include<algorithm> #define REP(i, a, b ...

  4. POJ——T 1422 Air Raid

    http://poj.org/problem?id=1422 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8579   A ...

  5. 国庆 day 1 下午

    一道图论好题(graph) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK有一张无向图G={V,E},这张无向图有n个点m条边组成.并且这是一张带权图, ...

  6. 基于json数据格式实现的简单数据库——jsonDB

    已在github上建立项目:https://github.com/ThinkerCodeChina/jsonDB /** +-------------------------------------- ...

  7. SVN管理工具的使用(实习第9天)

    最后就是我进行提交代码的时候要记得写注释,自己写了那些方法要标明,或者自己修改了哪些功能也要表明.不然我会被骂的. 2>代码冲突解决的问题: 服务器上的代码跟自己代码改动的地方不同 这种情况比较 ...

  8. codeforces@281 B

    shui #include<iostream> #include<cstdio> #include<cstring> #include<algorithm&g ...

  9. Vue Syntax Highlight

    Vue Syntax Highlight https://github.com/vuejs/vue-syntax-highlight

  10. SSD-tensorflow-1 demo

    一.简易识别 用最简单的已训练好的模型对20类目标做检测. 你电脑的tensorflow + CUDA + CUDNN环境都是OK的, 同时python需要安装cv2库 {      'aeropla ...