forked from iluwatar/java-design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refactor] Remove unnecessary declarations in service-locator pattern.
- Loading branch information
Showing
6 changed files
with
119 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 26 additions & 26 deletions
52
service-locator/src/main/java/com/iluwatar/ServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters