Sunday 8 September 2013

Service Locator Design Pattern - Implementation



Click here to watch in Youtube : https://www.youtube.com/watch?v=4MaBWg44fbs

Click the below Image to Enlarge





















Service.java

public interface Service
{
public void execute();
public String getName();
}


JMSService.java


public class JMSService implements Service
{

@Override
public void execute()
{
System.out.println("Executing JMSService");

}
@Override
public String getName()
{
return "JMSService";
}

}

EJBService.java


public class EJBService implements Service
{

@Override
public void execute()
{
System.out.println("Executing EJBService");
}

@Override
public String getName()
{
return "EJBService";
}

}

InitialContext.java


public class InitialContext
{
public Object lookup( String jndiName )
{
if( jndiName.equalsIgnoreCase("EJBService") )
{
// JNDI lookup code will come in real time implementation
System.out.println("Looking up and creating a new EJBService object");
return new EJBService();
}
else if( jndiName.equalsIgnoreCase("JMSService") )
{
   // JNDI lookup code will come in real time implementation
System.out.println("Looking up and creating a new JMSService object");
return new JMSService();
}
return null;
}
}

Cache.java


import java.util.ArrayList;
import java.util.List;

public class Cache
{
private List<Service> services;

public Cache()
{
services = new ArrayList<Service>();
}

public Service getService( String serviceName )
{
for( Service service : services )
{
if( service.getName().equalsIgnoreCase(serviceName) )
{
System.out.println("Return cached  " + serviceName + " object");
return service;
}
}
return null;
}

public void addService( Service newService )
{
boolean exists = false;
for( Service service : services )
{
if( service.getName().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 jndiName )
{
// First check the service object available in cache
Service service = cache.getService(jndiName);

if( service != null )
{
return service;
}

/*
* If service object not available in cache do the lookup using
* JNDI initial context and get the service object and add it to
* the cache for future use
*/
InitialContext context = new InitialContext();
Service service1 = (Service) context.lookup(jndiName);
cache.addService(service1);
return service1;
}
}


Client.java


public class Client
{

public static void main( String[] args )
{
Service service = ServiceLocator.getService("EJBService");
service.execute();
System.out.println();
service = ServiceLocator.getService("JMSService");
service.execute();
System.out.println();
service = ServiceLocator.getService("EJBService");
service.execute();
System.out.println();
service = ServiceLocator.getService("JMSService");
service.execute();

}

}

Output

Looking up and creating a new EJBService object
Executing EJBService

Looking up and creating a new JMSService object
Executing JMSService

Return cached  EJBService object
Executing EJBService

Return cached  JMSService object
Executing JMSService

No comments:

Post a Comment