Skip to content

Commit 1405808

Browse files
committed
[refactor] Remove unnecessary declarations in service-locator pattern.
1 parent 4b432a7 commit 1405808

File tree

6 files changed

+119
-120
lines changed

6 files changed

+119
-120
lines changed
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package com.iluwatar;
2+
23
/**
3-
* Service locator pattern, used to lookup jndi services
4-
* and cache them for subsequent requests.
5-
* @author saifasif
4+
* Service locator pattern, used to lookup jndi services
5+
* and cache them for subsequent requests.
66
*
7+
* @author saifasif
78
*/
89
public class App {
9-
public static void main(String[] args) {
10-
Service service = ServiceLocator.getService("jndi/serviceA");
11-
service.execute();
12-
service = ServiceLocator.getService("jndi/serviceB");
13-
service.execute();
14-
service = ServiceLocator.getService("jndi/serviceA");
15-
service.execute();
16-
service = ServiceLocator.getService("jndi/serviceA");
17-
service.execute();
18-
}
19-
10+
public static void main(String[] args) {
11+
Service service = ServiceLocator.getService("jndi/serviceA");
12+
service.execute();
13+
service = ServiceLocator.getService("jndi/serviceB");
14+
service.execute();
15+
service = ServiceLocator.getService("jndi/serviceA");
16+
service.execute();
17+
service = ServiceLocator.getService("jndi/serviceA");
18+
service.execute();
19+
}
2020
}

service-locator/src/main/java/com/iluwatar/InitContext.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@
33
/**
44
* For JNDI lookup of services from the web.xml. Will match name of the service name that
55
* is being requested and return a newly created service object with the name
6-
* @author saifasif
76
*
7+
* @author saifasif
88
*/
99
public class InitContext {
1010

11-
/**
12-
* Perform the lookup based on the service name. The returned object will need to be
13-
* casted into a {@link Service}
14-
* @param serviceName
15-
* @return
16-
*/
17-
public Object lookup(String serviceName){
18-
if( serviceName.equals("jndi/serviceA") ){
19-
System.out.println("Looking up service A and creating new service for A");
20-
return new ServiceImpl("jndi/serviceA");
21-
} else if( serviceName.equals("jndi/serviceB") ){
22-
System.out.println("Looking up service B and creating new service for B");
23-
return new ServiceImpl("jndi/serviceB");
24-
} else {
25-
return null;
26-
}
27-
}
28-
11+
/**
12+
* Perform the lookup based on the service name. The returned object will need to be
13+
* casted into a {@link Service}
14+
*
15+
* @param serviceName a string
16+
* @return an {@link Object}
17+
*/
18+
public Object lookup(String serviceName) {
19+
if (serviceName.equals("jndi/serviceA")) {
20+
System.out.println("Looking up service A and creating new service for A");
21+
return new ServiceImpl("jndi/serviceA");
22+
} else if (serviceName.equals("jndi/serviceB")) {
23+
System.out.println("Looking up service B and creating new service for B");
24+
return new ServiceImpl("jndi/serviceB");
25+
} else {
26+
return null;
27+
}
28+
}
2929
}

service-locator/src/main/java/com/iluwatar/Service.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ public interface Service {
1414
/*
1515
* The human readable name of the service
1616
*/
17-
public String getName();
17+
String getName();
1818

1919
/*
2020
* Unique ID of the particular service
2121
*/
22-
public int getId();
22+
int getId();
2323

2424
/*
2525
* The workflow method that defines what this service does
2626
*/
27-
public void execute();
28-
27+
void execute();
2928
}

service-locator/src/main/java/com/iluwatar/ServiceCache.java

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,43 @@
55

66
/**
77
* The service cache implementation which will cache services that are being created.
8-
* On first hit, the cache will be empty and thus any service that is being requested, will be
8+
* On first hit, the cache will be empty and thus any service that is being requested, will be
99
* created fresh and then placed into the cache map. On next hit, if same service name will
10-
* be requested, it will be returned from the cache
11-
* @author saifasif
10+
* be requested, it will be returned from the cache
1211
*
12+
* @author saifasif
1313
*/
1414
public class ServiceCache {
15-
16-
private Map<String, Service> serviceCache;
1715

18-
public ServiceCache() {
19-
serviceCache = new HashMap<String, Service>();
20-
}
16+
private final Map<String, Service> serviceCache;
17+
18+
public ServiceCache() {
19+
serviceCache = new HashMap<String, Service>();
20+
}
2121

22-
/**
23-
* Get the service from the cache. null if no service is found matching the
24-
* name
25-
* @param serviceName
26-
* @return {@link Service}
27-
*/
28-
public Service getService(String serviceName){
29-
Service cachedService = null;
30-
for (String serviceJndiName : serviceCache.keySet()){
31-
if( serviceJndiName.equals( serviceName ) ){
32-
cachedService = serviceCache.get(serviceJndiName);
33-
System.out.println("(cache call) Fetched service " + cachedService.getName() + "("+cachedService.getId()+") from cache... !");
34-
}
35-
}
36-
return cachedService;
37-
}
22+
/**
23+
* Get the service from the cache. null if no service is found matching the name
24+
*
25+
* @param serviceName a string
26+
* @return {@link Service}
27+
*/
28+
public Service getService(String serviceName) {
29+
Service cachedService = null;
30+
for (String serviceJndiName : serviceCache.keySet()) {
31+
if (serviceJndiName.equals(serviceName)) {
32+
cachedService = serviceCache.get(serviceJndiName);
33+
System.out.println("(cache call) Fetched service " + cachedService.getName() + "(" + cachedService.getId() + ") from cache... !");
34+
}
35+
}
36+
return cachedService;
37+
}
3838

39-
/**
40-
* Adds the service into the cache map
41-
* @param newService
42-
*/
43-
public void addService(Service newService){
44-
serviceCache.put(newService.getName(), newService);
45-
}
39+
/**
40+
* Adds the service into the cache map
41+
*
42+
* @param newService a {@link Service}
43+
*/
44+
public void addService(Service newService) {
45+
serviceCache.put(newService.getName(), newService);
46+
}
4647
}
Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
package com.iluwatar;
22

33
/**
4-
* This is a single service implementation of a sample service. This is the actual
5-
* service that will process the request. The reference for this service is to
4+
* This is a single service implementation of a sample service. This is the actual
5+
* service that will process the request. The reference for this service is to
66
* be looked upon in the JNDI server that can be set in the web.xml deployment descriptor
7-
* @author saifasif
87
*
8+
* @author saifasif
99
*/
1010
public class ServiceImpl implements Service {
11-
12-
private String serviceName;
13-
private int id;
14-
15-
public ServiceImpl(String serviceName) {
16-
// set the service name
17-
this.serviceName = serviceName;
18-
19-
// Generate a random id to this service object
20-
this.id = (int)Math.floor(Math.random()*1000)+1;
21-
}
2211

23-
@Override
24-
public String getName() {
25-
return serviceName;
26-
}
12+
private final String serviceName;
13+
private final int id;
14+
15+
public ServiceImpl(String serviceName) {
16+
// set the service name
17+
this.serviceName = serviceName;
18+
19+
// Generate a random id to this service object
20+
this.id = (int) Math.floor(Math.random() * 1000) + 1;
21+
}
22+
23+
@Override
24+
public String getName() {
25+
return serviceName;
26+
}
2727

28-
@Override
29-
public int getId() {
30-
return id;
31-
}
28+
@Override
29+
public int getId() {
30+
return id;
31+
}
3232

33-
@Override
34-
public void execute() {
35-
System.out.println("Service " + getName() + " is now executing with id " + getId());
36-
}
33+
@Override
34+
public void execute() {
35+
System.out.println("Service " + getName() + " is now executing with id " + getId());
36+
}
3737
}

service-locator/src/main/java/com/iluwatar/ServiceLocator.java

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,34 @@
33
/**
44
* The service locator module.
55
* Will fetch service from cache, otherwise creates a fresh service and update cache
6-
*
7-
* @author saifasif
86
*
7+
* @author saifasif
98
*/
109
public class ServiceLocator {
11-
12-
private static ServiceCache serviceCache = new ServiceCache();
13-
14-
/**
15-
* Fetch the service with the name param from the cache first,
16-
* if no service is found, lookup the service from the {@link InitContext} and
17-
* then add the newly created service into the cache map for future requests.
18-
* @param serviceJndiName
19-
* @return {@link Service}
20-
*/
21-
public static Service getService(String serviceJndiName){
22-
Service serviceObj = serviceCache.getService(serviceJndiName);
23-
if ( serviceObj != null ){
24-
return serviceObj;
25-
} else {
26-
/*
27-
* If we are unable to retrive anything from cache, then
10+
11+
private static ServiceCache serviceCache = new ServiceCache();
12+
13+
/**
14+
* Fetch the service with the name param from the cache first,
15+
* if no service is found, lookup the service from the {@link InitContext} and
16+
* then add the newly created service into the cache map for future requests.
17+
*
18+
* @param serviceJndiName a string
19+
* @return {@link Service}
20+
*/
21+
public static Service getService(String serviceJndiName) {
22+
Service serviceObj = serviceCache.getService(serviceJndiName);
23+
if (serviceObj != null) {
24+
return serviceObj;
25+
} else {
26+
/*
27+
* If we are unable to retrive anything from cache, then
2828
* lookup the service and add it in the cache map
2929
*/
30-
InitContext ctx = new InitContext();
31-
serviceObj = (Service) ctx.lookup(serviceJndiName);
32-
serviceCache.addService(serviceObj);
33-
return serviceObj;
34-
}
35-
}
36-
30+
InitContext ctx = new InitContext();
31+
serviceObj = (Service) ctx.lookup(serviceJndiName);
32+
serviceCache.addService(serviceObj);
33+
return serviceObj;
34+
}
35+
}
3736
}

0 commit comments

Comments
 (0)