课程表

设计模式课程

工具箱
速查手册

服务定位器模式

当前位置:免费教程 » Java相关 » 设计模式

服务定位器模式(Service Locator Pattern)用在我们想使用 JNDI 查询定位各种服务的时候。考虑到为某个服务查找 JNDI 的代价很高,服务定位器模式充分利用了缓存技术。在首次请求某个服务时,服务定位器在 JNDI 中查找服务,并缓存该服务对象。当再次请求相同的服务时,服务定位器会在它的缓存中查找,这样可以在很大程度上提高应用程序的性能。以下是这种设计模式的实体。

  • 服务(Service) - 实际处理请求的服务。对这种服务的引用可以在 JNDI 服务器中查找到。
  • Context / 初始的 Context - JNDI Context 带有对要查找的服务的引用。
  • 服务定位器(Service Locator) - 服务定位器是通过 JNDI 查找和缓存服务来获取服务的单点接触。
  • 缓存(Cache) - 缓存存储服务的引用,以便复用它们。
  • 客户端(Client) - Client 是通过 ServiceLocator 调用服务的对象。

实现

我们将创建 ServiceLocatorInitialContextCacheService 作为表示实体的各种对象。Service1Service2 表示实体服务。

ServiceLocatorPatternDemo,我们的演示类在这里是作为一个客户端,将使用 ServiceLocator 来演示服务定位器设计模式。

服务定位器模式的 UML 图

步骤 1

创建服务接口 Service。

Service.java

  1. public interface Service {
  2. public String getName();
  3. public void execute();
  4. }

步骤 2

创建实体服务。

Service1.java

  1. public class Service1 implements Service {
  2. public void execute(){
  3. System.out.println("Executing Service1");
  4. }
  5.  
  6. @Override
  7. public String getName() {
  8. return "Service1";
  9. }
  10. }

Service2.java

  1. public class Service2 implements Service {
  2. public void execute(){
  3. System.out.println("Executing Service2");
  4. }
  5.  
  6. @Override
  7. public String getName() {
  8. return "Service2";
  9. }
  10. }

步骤 3

为 JNDI 查询创建 InitialContext。

InitialContext.java

  1. public class InitialContext {
  2. public Object lookup(String jndiName){
  3. if(jndiName.equalsIgnoreCase("SERVICE1")){
  4. System.out.println("Looking up and creating a new Service1 object");
  5. return new Service1();
  6. }else if (jndiName.equalsIgnoreCase("SERVICE2")){
  7. System.out.println("Looking up and creating a new Service2 object");
  8. return new Service2();
  9. }
  10. return null;
  11. }
  12. }

步骤 4

创建缓存 Cache。

Cache.java

  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. public class Cache {
  5.  
  6. private List<Service> services;
  7.  
  8. public Cache(){
  9. services = new ArrayList<Service>();
  10. }
  11.  
  12. public Service getService(String serviceName){
  13. for (Service service : services) {
  14. if(service.getName().equalsIgnoreCase(serviceName)){
  15. System.out.println("Returning cached "+serviceName+" object");
  16. return service;
  17. }
  18. }
  19. return null;
  20. }
  21.  
  22. public void addService(Service newService){
  23. boolean exists = false;
  24. for (Service service : services) {
  25. if(service.getName().equalsIgnoreCase(newService.getName())){
  26. exists = true;
  27. }
  28. }
  29. if(!exists){
  30. services.add(newService);
  31. }
  32. }
  33. }

步骤 5

创建服务定位器。

ServiceLocator.java

  1. public class ServiceLocator {
  2. private static Cache cache;
  3.  
  4. static {
  5. cache = new Cache();
  6. }
  7.  
  8. public static Service getService(String jndiName){
  9.  
  10. Service service = cache.getService(jndiName);
  11.  
  12. if(service != null){
  13. return service;
  14. }
  15.  
  16. InitialContext context = new InitialContext();
  17. Service service1 = (Service)context.lookup(jndiName);
  18. cache.addService(service1);
  19. return service1;
  20. }
  21. }

步骤 6

使用 ServiceLocator 来演示服务定位器设计模式。

ServiceLocatorPatternDemo.java

  1. public class ServiceLocatorPatternDemo {
  2. public static void main(String[] args) {
  3. Service service = ServiceLocator.getService("Service1");
  4. service.execute();
  5. service = ServiceLocator.getService("Service2");
  6. service.execute();
  7. service = ServiceLocator.getService("Service1");
  8. service.execute();
  9. service = ServiceLocator.getService("Service2");
  10. service.execute();
  11. }
  12. }

步骤 7

验证输出。

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
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号