Files
abstract-document
abstract-factory
adapter
aggregator-microservices
api-gateway
async-method-invocation
balking
bridge
builder
business-delegate
caching
callback
chain
command
composite
converter
cqrs
dao
data-bus
data-mapper
data-transfer-object
decorator
delegation
dependency-injection
double-checked-locking
double-dispatch
eip-aggregator
eip-splitter
eip-wire-tap
event-aggregator
event-asynchronous
event-driven-architecture
event-queue
event-sourcing
execute-around
extension-objects
facade
factory-kit
factory-method
feature-toggle
fluentinterface
flux
flyweight
front-controller
guarded-suspension
half-sync-half-async
hexagonal
intercepting-filter
interpreter
iterator
layers
lazy-loading
marker
mediator
memento
message-channel
model-view-controller
model-view-presenter
module
monad
monostate
multiton
mute-idiom
mutex
naked-objects
null-object
object-mother
object-pool
observer
page-object
partial-response
poison-pill
private-class-data
producer-consumer
promise
property
prototype
proxy
publish-subscribe
queue-load-leveling
reactor
reader-writer-lock
repository
resource-acquisition-is-initialization
retry
semaphore
servant
serverless
service-layer
service-locator
etc
src
README.md
pom.xml
singleton
specification
state
step-builder
strategy
template-method
thread-pool
throttling
tls
tolerant-reader
trampoline
twin
unit-of-work
value-object
visitor
.gitignore
.travis.yml
CODE_COVERAGE.md
CONTRIBUTING.MD
LICENSE.md
PULL_REQUEST_TEMPLATE.md
README.md
checkstyle-suppressions.xml
checkstyle.xml
exclude-pmd.properties
faq.md
pom.xml
update-ghpages.sh
java-design-patterns/service-locator/README.md

44 lines
1.4 KiB
Markdown
Raw Normal View History

---
layout: pattern
title: Service Locator
folder: service-locator
permalink: /patterns/service-locator/
categories: Structural
2015-12-28 15:52:44 +02:00
tags:
- Java
- Difficulty-Beginner
- Performance
---
## Intent
Encapsulate the processes involved in obtaining a service with a
strong abstraction layer.
![alt text](./etc/service-locator.png "Service Locator")
## Applicability
The service locator pattern is applicable whenever we want
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.
## Typical Use Case
* when network hits are expensive and time consuming
* lookups of services are done quite frequently
* large number of services are being used
## 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.
## Credits
* [J2EE Design Patterns](http://www.amazon.com/J2EE-Design-Patterns-William-Crawford/dp/0596004273/ref=sr_1_2)