From 14058082bbc4639a0a91c7f1f19bb4370d0635d3 Mon Sep 17 00:00:00 2001 From: ruslanpa Date: Tue, 10 Feb 2015 10:02:44 +0200 Subject: [PATCH] [refactor] Remove unnecessary declarations in service-locator pattern. --- .../src/main/java/com/iluwatar/App.java | 28 ++++----- .../main/java/com/iluwatar/InitContext.java | 38 +++++------ .../src/main/java/com/iluwatar/Service.java | 7 +-- .../main/java/com/iluwatar/ServiceCache.java | 63 ++++++++++--------- .../main/java/com/iluwatar/ServiceImpl.java | 52 +++++++-------- .../java/com/iluwatar/ServiceLocator.java | 51 ++++++++------- 6 files changed, 119 insertions(+), 120 deletions(-) diff --git a/service-locator/src/main/java/com/iluwatar/App.java b/service-locator/src/main/java/com/iluwatar/App.java index 54635e9077b7..25ac36d6b526 100644 --- a/service-locator/src/main/java/com/iluwatar/App.java +++ b/service-locator/src/main/java/com/iluwatar/App.java @@ -1,20 +1,20 @@ package com.iluwatar; + /** - * Service locator pattern, used to lookup jndi services - * and cache them for subsequent requests. - * @author saifasif + * Service locator pattern, used to lookup jndi services + * and cache them for subsequent requests. * + * @author saifasif */ 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(); - } - + 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(); + } } diff --git a/service-locator/src/main/java/com/iluwatar/InitContext.java b/service-locator/src/main/java/com/iluwatar/InitContext.java index 8b217bc2816c..3c03b717a2e2 100644 --- a/service-locator/src/main/java/com/iluwatar/InitContext.java +++ b/service-locator/src/main/java/com/iluwatar/InitContext.java @@ -3,27 +3,27 @@ /** * 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 - */ - public Object lookup(String serviceName){ - if( serviceName.equals("jndi/serviceA") ){ - System.out.println("Looking up service A and creating new service for A"); - return new ServiceImpl("jndi/serviceA"); - } else if( serviceName.equals("jndi/serviceB") ){ - System.out.println("Looking up service B and creating new service for B"); - return new ServiceImpl("jndi/serviceB"); - } else { - return null; - } - } - + /** + * Perform the lookup based on the service name. The returned object will need to be + * casted into a {@link Service} + * + * @param serviceName a string + * @return an {@link Object} + */ + public Object lookup(String serviceName) { + if (serviceName.equals("jndi/serviceA")) { + System.out.println("Looking up service A and creating new service for A"); + return new ServiceImpl("jndi/serviceA"); + } else if (serviceName.equals("jndi/serviceB")) { + System.out.println("Looking up service B and creating new service for B"); + return new ServiceImpl("jndi/serviceB"); + } else { + return null; + } + } } diff --git a/service-locator/src/main/java/com/iluwatar/Service.java b/service-locator/src/main/java/com/iluwatar/Service.java index 6a6af8156ef5..e9642e3d0316 100644 --- a/service-locator/src/main/java/com/iluwatar/Service.java +++ b/service-locator/src/main/java/com/iluwatar/Service.java @@ -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(); } diff --git a/service-locator/src/main/java/com/iluwatar/ServiceCache.java b/service-locator/src/main/java/com/iluwatar/ServiceCache.java index f175583bac9c..4d5a388c37f0 100644 --- a/service-locator/src/main/java/com/iluwatar/ServiceCache.java +++ b/service-locator/src/main/java/com/iluwatar/ServiceCache.java @@ -5,42 +5,43 @@ /** * 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 + * 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 + * be requested, it will be returned from the cache * + * @author saifasif */ public class ServiceCache { - - private Map serviceCache; - public ServiceCache() { - serviceCache = new HashMap(); - } + private final Map serviceCache; + + public ServiceCache() { + serviceCache = new HashMap(); + } - /** - * Get the service from the cache. null if no service is found matching the - * name - * @param serviceName - * @return {@link Service} - */ - 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; - } + /** + * 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) { + 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 newService - */ - public void addService(Service newService){ - serviceCache.put(newService.getName(), newService); - } + /** + * Adds the service into the cache map + * + * @param newService a {@link Service} + */ + public void addService(Service newService) { + serviceCache.put(newService.getName(), newService); + } } diff --git a/service-locator/src/main/java/com/iluwatar/ServiceImpl.java b/service-locator/src/main/java/com/iluwatar/ServiceImpl.java index 425c60a24fac..d0f865248440 100644 --- a/service-locator/src/main/java/com/iluwatar/ServiceImpl.java +++ b/service-locator/src/main/java/com/iluwatar/ServiceImpl.java @@ -1,37 +1,37 @@ 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 + * 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; - - public ServiceImpl(String serviceName) { - // set the service name - this.serviceName = serviceName; - - // Generate a random id to this service object - this.id = (int)Math.floor(Math.random()*1000)+1; - } - @Override - public String getName() { - return serviceName; - } + private final String serviceName; + private final int id; + + public ServiceImpl(String serviceName) { + // set the service name + this.serviceName = serviceName; + + // Generate a random id to this service object + this.id = (int) Math.floor(Math.random() * 1000) + 1; + } + + @Override + public String getName() { + return serviceName; + } - @Override - public int getId() { - return id; - } + @Override + public int getId() { + return id; + } - @Override - public void execute() { - System.out.println("Service " + getName() + " is now executing with id " + getId()); - } + @Override + public void execute() { + System.out.println("Service " + getName() + " is now executing with id " + getId()); + } } diff --git a/service-locator/src/main/java/com/iluwatar/ServiceLocator.java b/service-locator/src/main/java/com/iluwatar/ServiceLocator.java index b8de9e3c20cf..7b6701f5f161 100644 --- a/service-locator/src/main/java/com/iluwatar/ServiceLocator.java +++ b/service-locator/src/main/java/com/iluwatar/ServiceLocator.java @@ -3,35 +3,34 @@ /** * The service locator module. * Will fetch service from cache, otherwise creates a fresh service and update cache - * - * @author saifasif * + * @author saifasif */ 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 {@link InitContext} and - * then add the newly created service into the cache map for future requests. - * @param serviceJndiName - * @return {@link Service} - */ - public static Service getService(String serviceJndiName){ - Service serviceObj = serviceCache.getService(serviceJndiName); - if ( serviceObj != null ){ - return serviceObj; - } else { - /* - * If we are unable to retrive anything from cache, then + + 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 {@link InitContext} and + * then add the newly created service into the cache map for future requests. + * + * @param serviceJndiName a string + * @return {@link Service} + */ + public static Service getService(String serviceJndiName) { + Service serviceObj = serviceCache.getService(serviceJndiName); + 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(serviceJndiName); - serviceCache.addService(serviceObj); - return serviceObj; - } - } - + InitContext ctx = new InitContext(); + serviceObj = (Service) ctx.lookup(serviceJndiName); + serviceCache.addService(serviceObj); + return serviceObj; + } + } }