Service Locator Pattern
The Service Locator Pattern is used when we want to locate various services using JNDI queries. Considering that performing a JNDI lookup for a service is expensive, the Service Locator Pattern makes full use of caching technology. When a service is first requested, 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 can greatly improve application performance. The following are the entities of this design pattern.
- Service- The service that actually handles requests. References to such services 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 a single point of contact for obtaining services through JNDI lookup and service caching.
- Cache- The cache stores references to services in order to reuse them.
- ClientClient is an 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 Case
- When an application needs to access multiple external services or resources and wants to centrally manage this access logic.
Implementation Approach
- Service Locator InterfaceDefine methods for obtaining services.
- Service Locator Implementation: Implement the service locator interface, encapsulating service lookup and access logic.
- Service InterfaceDefine the service interface or contract.
- Concrete Service: Implement the service interface, providing specific service functionality.
Key code
- Service locatorContains logic to find and return the requested service instance.
- Service InterfaceDefine an abstract representation of the service.
Application example
- Enterprise Application: The application uses the service locator to access database services, message queue services, etc.
Advantages
- Decouple service access: Separates service access logic from the business logic that uses the services.
- Centralized Management: Service access points are centrally managed, making maintenance and extension easier.
- FlexibilityEasy 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 misuse of the design pattern, thereby hiding the system structure.
Usage suggestions
- When an application needs to access multiple services in a flexible and maintainable way, consider using the Service Locator Pattern.
Notes
- Avoid over-reliance on the service locator, as it may mask the system's dependencies, making debugging and optimization difficult.
Contains several main roles
-
Service Locator Interface:
- Define methods for obtaining services.
-
Service Locator Implementation:
- Implement the service locator interface, encapsulating service lookup and access logic.
-
Service Interface:
- Define an abstract representation of the service.
-
Concrete Service:
- Implement the service interface, providing specific service functionality.
-
Client:
- Use the service locator to access the required services.
The Service Locator Pattern helps simplify service access logic and improve application flexibility and maintainability by providing a centralized service access point.
<
Implementation
We will createServiceLocator、InitialContext、Cache、ServiceVarious objects representing entities.Service1andService2Represents an entity service.
ServiceLocatorPatternDemoThe class here acts as a client and will useServiceLocatorTo demonstrate the Service Locator design pattern.
Step 1
Create the service interface Service.
Service.java
Step 2
Creates an entity service.
Service1.java
Service2.java
Step 3
Create an InitialContext for JNDI queries.
InitialContext.java
Step 4
Create a cache Cache.
Cache.java
Step 5
Create a service locator.
ServiceLocator.java
Step 6
UsageServiceLocatorTo demonstrate the Service Locator design pattern.
ServiceLocatorPatternDemo.java
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 Service2other extensions