//
//
// 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 908
Service 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
*/
java设计模式进阶_service-locator
最新推荐文章于 2022-03-16 14:06:23 发布