Service Locator Pattern
The Service Locator Pattern is used when we want to locate various services using JNDI lookups. Considering that looking up JNDI for a service is very expensive, the Service Locator Pattern makes full use of caching technology. When a service is requested for the first time, the service locator looks up the service in JNDI and caches the service object. When the same service is requested again, the service locator looks it up in its cache, which greatly improves the performance of the application. The following are the entities of this design pattern.
- Service - The service that actually handles requests. A reference to such a service can be found in the JNDI server.
- Context / Initial Context - The JNDI Context holds references to the services to be looked up.
- Service Locator - The service locator is the single point of contact for obtaining services through JNDI lookup and caching.
- Cache - The cache stores references to services so that they can be reused.
- Client - The Client is the object that invokes services through ServiceLocator.
Summary
Intent
Used to provide a centralized service access point in an application for obtaining various services or resources.
Main problem solved
- Solves the problem of avoiding hard-coded service access logic when an application needs to access various services or resources.
Use Cases
- When an application needs to access multiple external services or resources and wants to manage these access logic centrally.
Implementation
- Service Locator Interface: Defines the method for obtaining services.
- Service Locator Implementation: Implements the service locator interface and encapsulates the service lookup and access logic.
- Service Interface: Defines the interface or contract of a service.
- Concrete Service: Implements the service interface and provides specific service functions.
Key code
- Service Locator: Contains logic to find and return the requested service instance.
- Service Interface: Defines the abstract representation of a service.
Application Example
- Enterprise applications: Applications use the service locator to access database services, message queue services, etc.
Advantages
- Decoupled service access: Separates the service access logic from the business logic that uses the services.
- Centralized management: The service access points are managed centrally for easy maintenance and extension.
- Flexibility: Easy to add, modify, or replace services.
Disadvantages
- Performance Issues: The service locator may introduce performance overhead, especially when a lookup is performed on every service request.
- Overuse: May lead to abuse of the design pattern, thereby hiding the system structure.
Usage recommendation
- Consider using the Service Locator Pattern when an application needs to access multiple services in a flexible and maintainable way.
Notes
- Avoid over-relying on the service locator, because it may hide the system's dependency relationships, making debugging and optimization difficult.
Main roles included
-
Service Locator Interface:
-
Service Locator Implementation:
-
Service Interface:
-
Concrete Service:
-
Client:
The Service Locator Pattern helps simplify service access logic and improve the flexibility and maintainability of an application by providing a centralized service access point.
<
Create a class that implements
We will create ServiceLocator、InitialContext、Cache、Service As the various objects representing entities.Service1 and Service2 Represents the entity service.
ServiceLocatorPatternDemo The class here acts as a client and will use ServiceLocator to demonstrate the Service Locator design pattern.
Step 1
Create the service interface Service.
Service.java
public
interface
Service
{
public
String
getName
(
)
;
public
void
execute
(
)
;
}
Step 2
Create the entity service.
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 an InitialContext for the 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 the cache 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 the 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 ServiceLocator to demonstrate the 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
Run the program, output result:
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