套模板网站价格,百度营销大学,视觉上体验的网站,在线制作动态图片自动生成Java服务定位器模式 实现范例 服务定位器模式#xff08;Service Locator Pattern#xff09;用于想使用 JNDI 查询定位各种服务的时候考虑到为某个服务查找 JNDI 的代价很高#xff0c;服务定位器模式充分利用了缓存技术在首次请求某个服务时#xff0c;服务定位器在 JNDI… Java服务定位器模式 实现范例 服务定位器模式Service Locator Pattern用于想使用 JNDI 查询定位各种服务的时候考虑到为某个服务查找 JNDI 的代价很高服务定位器模式充分利用了缓存技术在首次请求某个服务时服务定位器在 JNDI 中查找服务并缓存该服务对象当再次请求相同的服务时服务定位器会在它的缓存中查找这样可以在很大程度上提高应用程序的性能以下是这种设计模式的实体 服务Service - 实际处理请求的服务。对这种服务的引用可以在 JNDI 服务器中查找到Context / 初始的 Context - JNDI Context 带有对要查找的服务的引用服务定位器Service Locator - 服务定位器是通过 JNDI 查找和缓存服务来获取服务的单点接触缓存Cache - 缓存存储服务的引用以便复用它们客户端Client - Client 是通过 ServiceLocator 调用服务的对象
实现 创建 ServiceLocator 、 InitialContext 、 Cache 、 Service 作为表示实体的各种对象Service1 和 Service2 表示实体服务ServiceLocatorPatternDemo 我们的演示类在这里是作为一个客户端将使用 ServiceLocator 来演示服务定位器设计模式
范例
1. 创建服务接口 Service
Service.java
package com.demo.gof;
public interface Service{public String getName();public void execute();
}2. 创建实体服务
Service1.java
package com.demo.gof;
public class Service1 implements Service {public void execute(){System.out.println(Executing Service1);}Overridepublic String getName() {return Service1;}
}Service2.java
package com.demo.gof;
public class Service2 implements Service {public void execute(){System.out.println(Executing Service2);}Overridepublic String getName() {return Service2;}
}3. 为 JNDI 查询创建 InitialContext
InitialContext.java
package com.demo.gof;
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; }
}4. 创建缓存 Cache
Cache.java
package com.demo.gof;
import java.util.ArrayList;
import java.util.List;public class Cache {private ListService services;public Cache(){services new ArrayListService();}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);}}
}
5. 创建服务定位器 ServiceLocator
ServiceLocator.java
package com.demo.gof;
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;}
}6. 使用 ServiceLocator 来演示服务定位器设计模式
ServiceLocatorPatternDemo.java
package com.demo.gof;
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(); }
}编译运行以上 Java 范例输出结果如下java
$ javac -d . src/main/com.demo/gof/ServiceLocatorPatternDemo.java
$ java com.demo.gof.ServiceLocatorPatternDemo
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