1.简介
菜鸟教程中:
服务定位器模式(Service Locator Pattern)用在我们想使用 JNDI 查询定位各种服务的时候。考虑到为某个服务查找 JNDI 的代价很高,服务定位器模式充分利用了缓存技术。在首次请求某个服务时,服务定位器在 JNDI 中查找服务,并缓存该服务对象。当再次请求相同的服务时,服务定位器会在它的缓存中查找,这样可以在很大程度上提高应用程序的性能。以下是这种设计模式的实体。服务(Service)- 实际处理请求的服务。对这种服务的引用可以在 JNDI 服务器中查找到。Context / 初始的 Context- JNDI Context 带有对要查找的服务的引用。服务定位器(Service Locator)- 服务定位器是通过 JNDI 查找和缓存服务来获取服务的单点接触。缓存(Cache)- 缓存存储服务的引用,以便复用它们。客户端(Client)- Client 是通过 ServiceLocator 调用服务的对象。
通过当前的服务定位器中的serviceLocator中获得服务资源,使用特定的词获得特定的service,(缓存中存在返回缓存数据,否者使用initContext获取service)
2.例子
模拟资源的查找
3.创建接口
Service中的内容:
/**
 * @description 服务接口
 * @author hy
 * @date 2019-09-03
 */
public interface Service {
	//服务名称
	String getName();
	
	//服务能够被调用
	void execute();
}
4.创建其他类
JNDIService中的内容:
/**
 * @description JNDI服务类
 * @author hy
 * @date 2019-09-03
 */
public class JNDIService implements Service {
	@Override
	public String getName() {
		return "jndi";
	}
	@Override
	public void execute() {
		System.out.println("访问当前的JNDIg服务!");
	}
}
DataSourceService中的内容:
/**
 * @description 数据源资源类
 * @author hy
 * @date 2019-09-03
 */
public class DataSourceService implements Service{
	@Override
	public String getName() {
		return "dataSource";
	}
	@Override
	public void execute() {
		System.out.println("获取数据链接资源!");
		
	}
}
InitContext中的内容:
/**
 * @description 初始化当前上下文的类
 * @author hy
 * @date 2019-09-03
 *
 */
public class InitContext {
	public Service lookUp(String name) {
		Service service=null;
		switch (name) {
		case "jndi":
			service=new JNDIService();
			break;
		default:			
			throw new IllegalArgumentException("没有 "+name+" 的服务!");
		}
		return service;
	}
}
Cache中的内容:
public class Cache {
	private Map<String, Service> cache=new HashMap<String, Service>();
	
	//获得缓存中的内容
	public Service getCache(String name) {
		Service service = cache.get(name);
		return service;
	}
	//添加缓存
	public void addCache(String name,Service service) {
		cache.put(name, service);
	}
}
ServiceLocator中的内容:
/**
 * @description 服务查找器
 * @author hy
 * @date 2019-09-04
 */
public class ServiceLocator {
	private Cache cache;
	private InitContext initContext;
	public ServiceLocator() {
		initContext=new InitContext();
		cache=new Cache();
	}
	//获得服务
	public Service getService(String name) {
		Service service = cache.getCache(name);
		if(service==null) {
			System.out.println("通过初始化资源获取当前的jndi资源!");
			service = initContext.lookUp(name);
			cache.addCache(name, service);
			System.out.println("将获取的资源加入缓存中!");
		}
		System.out.println("已经获取到了jndi资源!");
		return service;
	}
	
	
}
5.创建测试类
ServiceLocatorTest中的内容:
/**
 * @description 测试:服务定位模式
 * @author hy
 * @date 2019-09-03
 */
public class ServiceLocatorTest {
	
	public static void main(String[] args) {
		ServiceLocator locator=new ServiceLocator();
		Service service = locator.getService("jndi");
		service.execute();
		service = locator.getService("jndi");
		service.execute();
	}
}
结果:
通过初始化资源获取当前的jndi资源!
将获取的资源加入缓存中!
已经获取到了jndi资源!
访问当前的JNDI服务!
已经获取到了jndi资源!
访问当前的JNDI服务!
6.总结
1.服务定位器模式通过服务定位中使用缓存和initContext获取资源,缓存用来缓存获取的资源,initContext通过查找来获取资源!
以上纯属个人见解,如有问题请联系本人!
来源:https://blog.csdn.net/weixin_45492007/article/details/100531858