java设计模式进阶_service-locator
来源:互联网 发布:php join implode 编辑:程序博客网 时间:2024/05/23 16:53
////// Generated by StarUML(tm) Java Add-In//// @ Project : Untitled// @ File Name : Service.java// @ Date : 2016/9/2// @ Author : /////** * This is going to be the parent service interface which we will * use to create out services.All services will have a * <li>service name</li> * <li>unique id</li> * <li>execution work flow</li> * */public interface Service { /** * the human readable name of the service * @return */ public String getName(); /** * Unique ID of the particular service * @return */ public int getId(); /** * the workflow method that defines what this service does */ public void execute();}////// Generated by StarUML(tm) Java Add-In//// @ Project : Untitled// @ File Name : ServiceImpl.java// @ Date : 2016/9/2// @ Author : /////** * This is a single service implementation of a sample service.This is the actual * service that will process the request.The reference for this service is to * be looked upon in the JNDI server that can be set in the web.xml deployment descriptor * */public class ServiceImpl implements Service { private final String serviceName; private final int id; public ServiceImpl(String serviceName) { //set the service name this.serviceName = serviceName; //Generate a random id the this ervice object this.id = (int)Math.floor(Math.random()*1000) + 1; } public String getName() { return serviceName; } public int getId() { return id; } public void execute() { System.out.println("Service " + getName() + " is now executing with id " + getId()); }}import java.util.HashMap;import java.util.Map;////// Generated by StarUML(tm) Java Add-In//// @ Project : Untitled// @ File Name : ServiceCache.java// @ Date : 2016/9/2// @ Author : /////** * The service cache implementation which will cache services that are being created. * On first hit,the cache will be empty and thus any service that is being requested,will be * created fresh and the placed into the cache map.On next hit,if same service name will * be requested,it will be returned from the cache * */public class ServiceCache { private final Map<String,Service> serviceCache; public ServiceCache() { serviceCache = new HashMap<String,Service>(); } /** * Get the service from the cache.null if no service is found matching the name * */ public Service getService(String serviceName) { Service cachedService = null; for(String serviceJndiName : serviceCache.keySet()) { if(serviceJndiName.equals(serviceName)) { cachedService = serviceCache.get(serviceJndiName); System.out.println("(cache call) Fetched service " + cachedService.getName() + "(" + cachedService.getId() + ") from cache... !"); } } return cachedService; } /** * Adds the service into the cache map * @param service */ public void addService(Service service) { serviceCache.put(service.getName(),service); }}////// Generated by StarUML(tm) Java Add-In//// @ Project : Untitled// @ File Name : ServiceLocator.java// @ Date : 2016/9/2// @ Author : /////** * The service locator module. * Will fetch service from cache,otherwise creates a fresh service and update cache * */public class ServiceLocator { private static ServiceCache serviceCache = new ServiceCache(); /** * Fetch the service with the name param from the cache first, * if no service is found,lookup the service from the InitContext and * then add the newly created service into the cache map for future requests. * * @param serviceName * @return */ public static Service getService(String serviceName) { Service serviceObj = serviceCache.getService(serviceName); if(serviceObj != null) return serviceObj; else{ /** * If we are unable to retrive anything from cache,then * lookup the service and add it in the cache map */ InitContext ctx = new InitContext(); serviceObj = (Service)ctx.lookup(serviceName); serviceCache.addService(serviceObj);; return serviceObj; } }}////// Generated by StarUML(tm) Java Add-In//// @ Project : Untitled// @ File Name : InitContext.java// @ Date : 2016/9/2// @ Author : /////** * For JNDI lookup of services from the web.xml Will match name of the service name that * is being requested and return a newly created service object with the name * */public class InitContext { /** * Perform the lookup based on the service name.The returned object will need to be * casted into a Service * @param serviceName * @return */ public Object lookup(String serviceName) { if (serviceName.equals("jndi/serviceA")) { return new ServiceImpl("jndi/serviceA"); } else if (serviceName.equals("jndi/serviceB")) { return new ServiceImpl("jndi/serviceB"); } else { return null; } }}/** * Service locator pattern,used to lookup jndi services * and cache them for subsequent requests. * @author Administrator * */public class App { public static void main(String[] args) { Service service = ServiceLocator.getService("jndi/serviceA"); service.execute(); service = ServiceLocator.getService("jndi/serviceB"); service.execute(); service = ServiceLocator.getService("jndi/serviceA"); service.execute(); service = ServiceLocator.getService("jndi/serviceA"); service.execute(); }}/*Service jndi/serviceA is now executing with id 908Service jndi/serviceB is now executing with id 893(cache call) Fetched service jndi/serviceA(908) from cache... !Service jndi/serviceA is now executing with id 908(cache call) Fetched service jndi/serviceA(908) from cache... !Service jndi/serviceA is now executing with id 908*/
0 0
- java设计模式进阶_service-locator
- java设计模式进阶_service-layer
- Java进阶-设计模式
- Java Service Locator Pattern(服务器定位模式)
- java设计模式进阶_singleton
- java设计模式进阶_prototype
- java设计模式进阶_builder
- java设计模式进阶_decorator
- java设计模式进阶_strategy
- java设计模式进阶_visitor
- java设计模式进阶_flyweight
- java设计模式进阶_proxy
- java设计模式进阶_facade
- java设计模式进阶_composite
- java设计模式进阶_bridge
- java设计模式进阶_adapter
- java设计模式进阶_interpreter
- java设计模式进阶_mediator
- ASP.NET开发过程中遇到的问题-控件找不到
- CCF-201604-4 游戏(三维BFS)
- vulkan 教程--Development environmen
- 学习记录5
- configure: error: you must configure in a separate build directory
- java设计模式进阶_service-locator
- 如何删除受权限保护的文件以及文件夹
- 个人收藏有用的Blog(不断更新)
- Java线程:线程状态的转换
- 广州颜鉴信息科技有限公司早报--人脸识别不安全了?美国科学家用VR技术破解
- ios 设备唯一标示符
- 学习记录4
- mysql允许远程连接的配置
- 线程安全和不安全的分析