Service Locator Pattern 服务定位
https://www.geeksforgeeks.org/service-locator-pattern/



Service Locator Pattern
Last Updated: 06-03-2018
The service locator pattern is a design pattern used in software development to encapsulate the processes involved in obtaining a service with a strong abstraction layer. This pattern uses a central registry known as the “service locator” which on request returns the information necessary to perform a certain task.
The ServiceLocator is responsible for returning instances of services when they are requested for by the service consumers or the service clients.
- Service Locator : The Service Locator abstracts the API lookup services, vendor dependencies, lookup complexities, and business object creation, and provides a simple interface to clients. This reduces the client’s complexity. In addition, the same client or other clients can reuse the Service Locator.
- InitialContext : The InitialContext object is the start point in the lookup and creation process. Service providers provide the context object, which varies depending on the type of business object provided by the Service Locator’s lookup and creation service.
- ServiceFactory : The ServiceFactory object represents an object that provides life cycle management for the BusinessService objects. The ServiceFactory object for enterprise beans is an EJBHome object.
- BusinessService : The BusinessService is a role that is fulfilled by the service the client is seeking to access. The BusinessService object is created or looked up or removed by the ServiceFactory. The BusinessService object in the context of an EJB application is an enterprise bean.
Suppose classes with dependencies on services whose concrete types are specified at compile time.
In the above diagram, ClassA has compile time dependencies on ServiceA and ServiceB.But this situation has drawbacks.
- If we want to replace or update the dependencies we must change the classes source code and recompile the solution.
- The concrete implementation of the dependencies must be available at compile time.
By using the Service Locator pattern :
In simple words, Service Locator pattern does not describe how to instantiate the services. It describes a way to register services and locate them.
Let’s see an example of Service Locator Pattern.
edit
play_arrow
brightness_4
// Java program to // illustrate Service Design Service // Locator Pattern import java.util.ArrayList; import java.util.List; // Service interface // for getting name and // Executing it. interface Service { public String getName(); public void execute(); } // Service one implementing Locator class ServiceOne implements Service { public void execute() { System.out.println("Executing ServiceOne"); } @Override public String getName() { return "ServiceOne"; } } // Service two implementing Locator class ServiceTwo implements Service { public void execute() { System.out.println("Executing ServiceTwo"); } @Override public String getName() { return "ServiceTwo"; } } // Checking the context // for ServiceOne and ServiceTwo class InitialContext { public Object lookup(String name) { if (name.equalsIgnoreCase("ServiceOne")) { System.out.println("Creating a new ServiceOne object"); return new ServiceOne(); } else if (name.equalsIgnoreCase("ServiceTwo")) { System.out.println("Creating a new ServiceTwo object"); return new ServiceTwo(); } return null; } } 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); } } } // Locator class class ServiceLocator { private static Cache cache; static { cache = new Cache(); } public static Service getService(String name) { Service service = cache.getService(name); if (service != null) { return service; } InitialContext context = new InitialContext(); Service ServiceOne = (Service)context.lookup(name); cache.addService(ServiceOne); return ServiceOne; } } // Driver class class ServiceLocatorPatternDemo { public static void main(String[] args) { Service service = ServiceLocator.getService("ServiceOne"); service.execute(); service = ServiceLocator.getService("ServiceTwo"); service.execute(); service = ServiceLocator.getService("ServiceOne"); service.execute(); service = ServiceLocator.getService("ServiceTwo"); service.execute(); } } |
Output:
Creating a new ServiceOne object
Executing ServiceOne
Creating a new ServiceTwo object
Executing ServiceTwo
Returning cached ServiceOne object
Executing ServiceOne
Returning cached ServiceTwo object
Executing ServiceTwo
Advantages :
- Applications can optimize themselves at run-time by selectively adding and removing items from the service locator.
- Large sections of a library or application can be completely separated. The only link between them becomes the registry.
Disadvantages :
- The registry makes the code more difficult to maintain (opposed to using Dependency injection), because it becomes unclear when you would be introducing a breaking change.
- The registry hides the class dependencies causing run-time errors instead of compile-time errors when dependencies are missing.
Strategies
The following strategies are used to implement service Locator Pattern :
- EJB Service Locator Strategy : This strategy uses EJBHome object for enterprise bean components and this EJBHome is cached in the ServiceLocator for future use when the client needs the home object again.
- JMS Queue Service Locator Strategy : This strategy is applicable to point to point messaging requirements. The following the strategies under JMS Queue Service Locator Strategy.
- JMS Queue Service Locator Strategy
- JMS Topic Service Locator Strategy
- Type Checked Service Locator Strategy : This strategy has trade-offs. It reduces the flexibility of lookup, which is in the Services Property Locator strategy, but add the type checking of passing in a constant to the ServiceLocator.getHome() method.
Design Pattern - Service Locator Pattern - Tutorialspoint https://www.tutorialspoint.com/design_pattern/service_locator_pattern.htm

Service Locator Pattern 服务定位的更多相关文章
- .NET 服务器定位模式(Service Locator Pattern)——Common Service Locator
本文内容 场景 目标 解决方案 实现细节 思考 相关模式 更多信息 参考资料 Common Service Locator 代码很简单,它一般不会单独使用,而是作为一个单件模式,与像 .net Uni ...
- [Design Pattern] Service Locator Pattern 简单案例
Service Locator Pattern,即服务定位模式,用于定位不同的服务.考虑到 InitialContext::lookup 的成本比较高,提供了 Cache 类缓存以定位到的服务. 代码 ...
- 《Prism 5.0源码走读》Service Locator Pattern
在Prism Bootstrapper里面取实例的时候使用 ServiceLocator模式,使用的是CommonServiceLocator库 (http://commonservicelocato ...
- 服务定位器(Service Locator)
服务定位器(Service Locator) 跟DI容器类似,引入Service Locator目的也在于解耦.有许多成熟的设计模式也可用于解耦,但在Web应用上, Service Locator绝对 ...
- Design Pattern - Service Locator Pattern--转载
原文地址:http://www.tutorialspoint.com/design_pattern/service_locator_pattern.htm The service locator de ...
- Service Locator 模式
什么是Service Locator 模式? 服务定位模式(Service Locator Pattern)是一种软件开发中的设计模式,通过应用强大的抽象层,可对涉及尝试获取一个服务的过程进行封装.该 ...
- Atitit。如何实现dip, di ,ioc ,Service Locator的区别于联系
Atitit.如何实现dip, di ,ioc ,Service Locator的区别于联系 1. Dip原则又来自于松耦合思想方向1 2. 要实现dip原则,有以下俩个模式1 3. Ioc和di的 ...
- 【转】Understanding Inversion of Control, Dependency Injection and Service Locator Print
原文:https://www.dotnettricks.com/learn/dependencyinjection/understanding-inversion-of-control-depende ...
- PHP中应用Service Locator服务定位及单例模式
单例模式将一个对象实例化后,放在静态变量中,供程序调用. 服务定位(ServiceLocator)就是对象工场Factory,调用者对象直接调用Service Locator,与被调用对象减轻了依赖关 ...
随机推荐
- ASP.NET Core 中间件 自定义全局异常中间件以及 MVC异常过滤器作用
中间件是一种装配到应用管道以处理请求和响应的软件. 每个组件: 选择是否将请求传递到管道中的下一个组件. 可在管道中的下一个组件前后执行工作. 请求委托用于生成请求管道. 请求委托处理每个 HTTP ...
- Core3.0返回的Json数据大小写格式问题
前言 测试发现,CoreWebAPI返回的Json数据,会将字段的首字母转换为小写, 经百度得,返回数据会默认驼峰命名,导致的. 随即百度, https://www.cnblogs.com/cdone ...
- python初学者-判断今天是今年的第几天代码
判断今天是今年的第几天源代码 import time date =time.localtime() year,month,day=date[:3] day_month=[31,28,31,30,31, ...
- 【红日安全-VulnStack】ATT&CK实战系列——红队实战(二)
一.环境搭建 1.1 靶场下载 靶场下载地址:http://vulnstack.qiyuanxuetang.net/vuln/detail/3/ 靶机通用密码: 1qaz@WSX 1.2 环境配置 ...
- 【官方免费】Apple Silicon M1 + Parallels 16技术预览版 + Win 10 arm64
期待了好久,终于能用pd运行win10了,其实也就想写个c++,mac上配置个c++编译器太麻烦了.. 步骤: 打开 https://my.parallels.com/desktop/beta,这里下 ...
- Redis中的常用命令哪些?
a.hset 存储一个哈希键值对的集合 b.hget获取一个哈希键的值c.hdel 删除一个或多个字段 d.hgetall 获取一个哈希是键值对的集合 e.lpush key value向链表左侧添加 ...
- Java学习日报7.16
void StudentManage::Sort() //排序功能{ StudentInfo *h,*curr,*temp,*last; h=head; for(int j=0;j<n;j++) ...
- JVM——GC(垃圾回收)算法
一.垃圾回收的基本概念 垃圾回收(GC,Garbage Collection),指内存中不会再被使用的对象清理掉. 垃圾回收有很多种算法:如引用计数法.标记压缩法.复制算法.分代/分区的思想 二.垃圾 ...
- 第七章节 BJROBOT 选择区域自主构建地图【ROS全开源阿克曼转向智能网联无人驾驶车】
1.把小车平放在地板上,用资料里的虚拟机,打开一个终端 ssh 过去主控端启动roslaunch znjrobot bringup.launch 2.在虚拟机端再打开一个终端,ssh 过去主控端启动r ...
- PO,BO,VO,DTO,POJO,DAO,DO是什么?
PO (Persistent Object) 持久化对象,表示实体数据.BO (Business Object) 业务对象,主要是把逻辑业务封装为一个对象 .VO (Value/Vi ...