Search This Blog

Monday 10 October 2016

Service locator design pattern

Service locator design pattern: 
It is used when we want to locate various services objects like JNDI. It caches the service objects. It looks first for service objects its cache. It access service object from its cache if it exists in cache otherwise  it lookup service object, it adds to its cache and returns service object.

Service - This is the actual service which process the request.

Service Locator - This is central point to get service object and it caches the services.

Cache - This holds references of services to reuse them.

Client - This calls the ServiceLocator and rceives the service object.

Servicelocator Demo:

BankService.java
-----------------------
public interface BankService {
 public String getServiceName();
 public boolean createAccount(CustmorInfo custInfo);
 public boolean deleteAccount(CustmorInfo custInfo);
}

AbcBankService.java
----------------------------
public class AbcBankService implements BankService {
public boolean createAccount(CustmorInfo custInfo){
boolean accountCreationStatus = false;
if (custInfo.getName()!= null) {
System.out.println("Account is created successfully in AbcBank");
accountCreationStatus = true;
}
retur accountCreationStatus;
}
public boolean deleteAccount(CustmorInfo custInfo) {
boolean accountDeletionStatus = false;
if (custInfo.getName()!= null) {
System.out.println("Account is delted successfully from AbcBank");
accountDeletionStatus = true;
}
retur accountDeletionStatus;
}
    @Override
public String getServiceName() {
    return "AbcBankService";
}
}

XyzBankService.java
----------------------------
public class XyzBankService implements BankService {
public boolean createAccount(CustmorInfo custInfo){
boolean accountCreationStatus = false;
if (custInfo.getName()!= null) {
System.out.println("Account is created successfully in XyzBank");
accountCreationStatus = true;
}
retur accountCreationStatus;
}
public boolean deleteAccount(CustmorInfo custInfo) {
boolean accountDeletionStatus = false;
if (custInfo.getName()!= null) {
System.out.println("Account is delted successfully from XyzBank");
accountDeletionStatus = true;
}
retur accountDeletionStatus;
}
    @Override
public String getServiceName() {
    return "AbcBankService";
}
}
CustmorInfo.java
------------------------
class CustmorInfo {
String name;
String address;
String accountType;

public setName(String _name) {
name = _name;
}
public String getName {
rturn name;
}
public void setAddress(String _address) {
address = _address;
}
public String getAddress() {
return address;
}
public void setAccountType(String _accountType) {
accountType = _accountType;
}
public String getAccountType() {
return accountType;
}
}

Cache.java
--------------
import java.util.ArrayList;
import java.util.List;
public class Cache {

  private List<BankService> services;
    public Cache(){    
services = new ArrayList<BankService>();
}
    public Service getService(String serviceName){
        for (BankService service : services) {
if(service.getServiceName().equalsIgnoreCase(serviceName)){
           System.out.println("Returning cached  " + serviceName + " object");
           return service;      
}    
}    
return null;
}
 
public void addService(BankService newService){    
boolean exists = false;          
for (BankService service : services) {      
if(service.getServiceName().equalsIgnoreCase(newService.getName())){          
exists = true;      
}    
}    
if(!exists){      
services.add(newService);    
}
}
}
ServiceLocator.java
--------------------------
public class ServiceLocator {
private static Cache cache;
    static {
     cache = new Cache();  
}
    public static Service getService(String serviceName){
      Service service = cache.getService(serviceName);
      if(service != null){      
return service;    
}
      //Make lookup calls to get service objects from JNDI  
Service service1 = BankServiceFactory.buildService(serviceName);
try {    
cache.addService(service1);
} catch (InvalidBankServiceException exception) {
System.out.println("Requested service is invalid" + exception);
}  
return service1;
}
}
BankServiceFactory.java
--------------------------------
class BankServiceFactory {
public static BankService buildService(serviceName) throws InvalidBankServiceException{
if ("AbcBankService".equalsIngnoreCase(serviceName)) {
return new AbcBankService();
} else if ("XyzBankService".equalsIngnoreCase(serviceName)) {
return new XyzBankService();
} else {
throw new InvalidBankServiceException("Requested bank service is invalid:::"+serviceName);
}

}
}

InvalidBankServiceException.java
---------------------------------------------
class InvalidBankServiceException{
InvalidBankServiceException(String s) {
     super(s);
}
}
ServiceLocatorDesignPatternDemo.java
----------------------------------------------------
public class ServiceLocatorDesignPatternDemo {
public static void main(String[] args) {    
Service service = ServiceLocator.getService("AbcBankService");
if (service != null) {    
service.createAccount();
}    
service = ServiceLocator.getService("XyzBankService");    
if (service != null) {    
service.createAccount();    
}
service = ServiceLocator.getService("AbcBankService");    
if (service != null) {    
service.deleteAccount();    
}
service = ServiceLocator.getService("XyzBankService");    
{
service.deleteAccount();  
}
}
}















No comments:

Post a Comment