[refactor] Remove unnecessary declarations in service-locator pattern.
This commit is contained in:
parent
4b432a79d9
commit
14058082bb
@ -1,9 +1,10 @@
|
||||
package com.iluwatar;
|
||||
|
||||
/**
|
||||
* Service locator pattern, used to lookup jndi services
|
||||
* and cache them for subsequent requests.
|
||||
* @author saifasif
|
||||
*
|
||||
* @author saifasif
|
||||
*/
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
@ -16,5 +17,4 @@ public class App {
|
||||
service = ServiceLocator.getService("jndi/serviceA");
|
||||
service.execute();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,16 +3,17 @@ package com.iluwatar;
|
||||
/**
|
||||
* 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
|
||||
* @author saifasif
|
||||
*
|
||||
* @author saifasif
|
||||
*/
|
||||
public class InitContext {
|
||||
|
||||
/**
|
||||
* Perform the lookup based on the service name. The returned object will need to be
|
||||
* casted into a {@link Service}
|
||||
* @param serviceName
|
||||
* @return
|
||||
*
|
||||
* @param serviceName a string
|
||||
* @return an {@link Object}
|
||||
*/
|
||||
public Object lookup(String serviceName) {
|
||||
if (serviceName.equals("jndi/serviceA")) {
|
||||
@ -25,5 +26,4 @@ public class InitContext {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -14,16 +14,15 @@ public interface Service {
|
||||
/*
|
||||
* The human readable name of the service
|
||||
*/
|
||||
public String getName();
|
||||
String getName();
|
||||
|
||||
/*
|
||||
* Unique ID of the particular service
|
||||
*/
|
||||
public int getId();
|
||||
int getId();
|
||||
|
||||
/*
|
||||
* The workflow method that defines what this service does
|
||||
*/
|
||||
public void execute();
|
||||
|
||||
void execute();
|
||||
}
|
||||
|
@ -8,21 +8,21 @@ import java.util.Map;
|
||||
* On first hit, the cache will be empty and thus any service that is being requested, will be
|
||||
* created fresh and then placed into the cache map. On next hit, if same service name will
|
||||
* be requested, it will be returned from the cache
|
||||
* @author saifasif
|
||||
*
|
||||
* @author saifasif
|
||||
*/
|
||||
public class ServiceCache {
|
||||
|
||||
private Map<String, Service> 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
|
||||
* @param serviceName
|
||||
* Get the service from the cache. null if no service is found matching the name
|
||||
*
|
||||
* @param serviceName a string
|
||||
* @return {@link Service}
|
||||
*/
|
||||
public Service getService(String serviceName) {
|
||||
@ -38,7 +38,8 @@ public class ServiceCache {
|
||||
|
||||
/**
|
||||
* Adds the service into the cache map
|
||||
* @param newService
|
||||
*
|
||||
* @param newService a {@link Service}
|
||||
*/
|
||||
public void addService(Service newService) {
|
||||
serviceCache.put(newService.getName(), newService);
|
||||
|
@ -4,13 +4,13 @@ package com.iluwatar;
|
||||
* 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
|
||||
* @author saifasif
|
||||
*
|
||||
* @author saifasif
|
||||
*/
|
||||
public class ServiceImpl implements Service {
|
||||
|
||||
private String serviceName;
|
||||
private int id;
|
||||
private final String serviceName;
|
||||
private final int id;
|
||||
|
||||
public ServiceImpl(String serviceName) {
|
||||
// set the service name
|
||||
|
@ -5,7 +5,6 @@ package com.iluwatar;
|
||||
* Will fetch service from cache, otherwise creates a fresh service and update cache
|
||||
*
|
||||
* @author saifasif
|
||||
*
|
||||
*/
|
||||
public class ServiceLocator {
|
||||
|
||||
@ -15,7 +14,8 @@ public class ServiceLocator {
|
||||
* Fetch the service with the name param from the cache first,
|
||||
* if no service is found, lookup the service from the {@link InitContext} and
|
||||
* then add the newly created service into the cache map for future requests.
|
||||
* @param serviceJndiName
|
||||
*
|
||||
* @param serviceJndiName a string
|
||||
* @return {@link Service}
|
||||
*/
|
||||
public static Service getService(String serviceJndiName) {
|
||||
@ -33,5 +33,4 @@ public class ServiceLocator {
|
||||
return serviceObj;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user