2015-08-13 23:54:40 +02:00
|
|
|
---
|
|
|
|
layout: pattern
|
|
|
|
title: Service Locator
|
|
|
|
folder: service-locator
|
2015-08-15 18:03:05 +02:00
|
|
|
permalink: /patterns/service-locator/
|
2015-08-20 21:40:07 +02:00
|
|
|
categories: Structural
|
2015-12-28 15:52:44 +02:00
|
|
|
tags:
|
|
|
|
- Java
|
|
|
|
- Difficulty-Beginner
|
2015-12-28 16:07:43 +02:00
|
|
|
- Performance
|
2015-08-13 23:54:40 +02:00
|
|
|
---
|
|
|
|
|
2016-01-03 21:14:30 +01:00
|
|
|
## Intent
|
|
|
|
Encapsulate the processes involved in obtaining a service with a
|
2015-08-13 23:54:40 +02:00
|
|
|
strong abstraction layer.
|
|
|
|
|
2015-09-24 12:23:02 +05:30
|
|
|

|
2015-08-13 23:54:40 +02:00
|
|
|
|
2016-01-03 21:14:30 +01:00
|
|
|
## Applicability
|
|
|
|
The service locator pattern is applicable whenever we want
|
2015-08-13 23:54:40 +02:00
|
|
|
to locate/fetch various services using JNDI which, typically, is a redundant
|
|
|
|
and expensive lookup. The service Locator pattern addresses this expensive
|
|
|
|
lookup by making use of caching techniques ie. for the very first time a
|
|
|
|
particular service is requested, the service Locator looks up in JNDI, fetched
|
|
|
|
the relevant service and then finally caches this service object. Now, further
|
|
|
|
lookups of the same service via Service Locator is done in its cache which
|
|
|
|
improves the performance of application to great extent.
|
|
|
|
|
2016-01-03 21:14:30 +01:00
|
|
|
## Typical Use Case
|
2015-08-13 23:54:40 +02:00
|
|
|
|
|
|
|
* when network hits are expensive and time consuming
|
|
|
|
* lookups of services are done quite frequently
|
2015-08-15 18:03:05 +02:00
|
|
|
* large number of services are being used
|
2015-09-24 12:23:02 +05:30
|
|
|
|
2016-10-03 21:59:36 +02:00
|
|
|
## Consequences
|
|
|
|
|
|
|
|
* Violates Interface Segregation Principle (ISP) by providing pattern consumers with an access
|
|
|
|
to a number of services that they don't potentially need.
|
|
|
|
* Creates hidden dependencies that can break the clients at runtime.
|
|
|
|
|
2016-01-03 21:14:30 +01:00
|
|
|
## Credits
|
2015-09-24 12:23:02 +05:30
|
|
|
|
|
|
|
* [J2EE Design Patterns](http://www.amazon.com/J2EE-Design-Patterns-William-Crawford/dp/0596004273/ref=sr_1_2)
|