Java Service Locator Pattern(服务器定位模式)
来源:互联网 发布:python基础环境搭建 编辑:程序博客网 时间:2024/05/19 13:08
Java Service Locator Pattern(服务器定位模式)
服务定位器模式(Service Locator Pattern)用在我们想使用 JNDI 查询定位各种服务的时候。考虑到为某个服务查找 JNDI 的代价很高,服务定位器模式充分利用了缓存技术。在首次请求某个服务时,服务定位器在 JNDI 中查找服务,并缓存该服务对象。当再次请求相同的服务时,服务定位器会在它的缓存中查找,这样可以在很大程度上提高应用程序的性能。以下是这种设计模式的实体。
- 服务(Service) - 实际处理请求的服务。对这种服务的引用可以在 JNDI 服务器中查找到。
Context / 初始的 Context - JNDI Context 带有对要查找的服务的引用。 - 服务定位器(Service Locator) - 服务定位器是通过 JNDI 查找和缓存服务来获取服务的单点接触。
- 缓存(Cache) - 缓存存储服务的引用,以便复用它们。
- 客户端(Client) - Client 是通过 ServiceLocator 调用服务的对象。
- 创建服务接口 Service。
/** * 1. 创建服务接口 Service。 * @author mazaiting */public interface Service { public String getName(); public void execute();}
- 创建实体服务。
/** * 2. 创建实体服务。 * @author mazaiting */public class Service1 implements Service{ public String getName() { return "Service1"; } public void execute() { System.out.println("Executing Service1"); }}/** * 2. 创建实体服务。 * @author mazaiting */public class Service2 implements Service{ public String getName() { return "Service2"; } public void execute() { System.out.println("Executing Service2"); }}
- 为 JNDI 查询创建 InitialContext。
/** * 3. 为 JNDI 查询创建 InitialContext。 * @author mazaiting */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; }}
- 创建缓存 Cache。
/** * 4. 创建缓存 Cache。 * @author mazaiting */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 "+service+" 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. 创建服务定位器。 * @author mazaiting */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; } }
- 使用 ServiceLocator 来演示服务定位器设计模式。
/** * 6. 使用 ServiceLocator 来演示服务定位器设计模式。 * @author mazaiting */public class Client { 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(); } }
- 打印结果
Looking up and creating a new Service1 objectExecuting Service1Looking up and creating a new Service2 objectExecuting Service2Returning cached com.mazaiting.serloc.Service1@15db9742 objectExecuting Service1Returning cached com.mazaiting.serloc.Service2@6d06d69c objectExecuting Service2
阅读全文
0 0
- Java Service Locator Pattern(服务器定位模式)
- 设计模式【服务器定位模式Service Locator Pattern】
- 设计模式学习—服务定位模式(Service Locator Design Pattern)
- Service Locator Pattern
- Service Locator Design Pattern
- [Design Pattern] Service Locator Pattern
- 【设计模式】服务定位器模式(Service Locator Pattern)
- 服务定位器模式(service locator)
- Service Locator 模式 的学习
- 服务定位器模式(Service Locator Pattern)详解和代码示范
- 用SERVICE LOCATOR 模式实现命名访问
- 用SERVICE LOCATOR 模式实现命名访问
- 使用service locator快速定位你的服务
- java设计模式进阶_service-locator
- 用SERVICE LOCATOR 模式实现访问命名服务
- 用SERVICE LOCATOR 模式实现访问命名服务
- Common Service Locator library
- Common Service Locator
- Java 邮件(问题解决)
- Java 邮件(JavaMail)
- Java Pinyin4j(汉字转拼音)
- Java Front Controller Pattern(前端控制器模式)
- Java Intercepting Filter Pattern(拦截过滤器)
- Java Service Locator Pattern(服务器定位模式)
- Java Transfer Object Pattern(传输对象模式)
- Android Exception(全局异常)
- 我的开源包
- MySQLProxy实现MySQL的读写分离
- GitHub优秀开源工具---Android
- 优秀的开源项目
- Eclipse 运行Openfire源码
- Openfire服务器配置