lixj0571
发贴: 84
积分: 2
|
于 2003-03-25 13:10
Applicability This pattern is applicable when you have a lot of stateless session beans in your application and you want to have a uniform way of accessing them
Participants * Service interface - This is a dummy interface which all Specific Services should extend. * SpecificService interface - This interface contains the declaration of all the business methods that usually go into a remote/local interface * SpecificSession interface - This interface just extends the SpecificService and ( EJBObject OR EJBLocalObject) * SpecificSessionHome interface - Just like any other home interface * SpecificSessionBean class - Just like any other session bean, implements SessionBean * ServiceLocator class - This has static methods getEJBLocalHome()/getEJBHome() [ This one is used to have the ability to cache contexts and home interfaces(). See ServiceLocator Pattern. ] * StatlessServiceFactory class - This contains a static method getService() which takes an argument that indicates what service the client is looking for. The argument could be a JNDI name or some other constant, that could ultimately be mapped to a JNDI Name/Class. * Client - Client that uses the factory class to locate the service
Implementation Here's the partial reference implementation for the above participants.
Service public interface Service { }
SpecificService public interface HelloService extends Service { public void sayHello() throws ...; public void sayBye() throws ...; }
SpecificSession public interface Hello extends HelloService,EJBLocalHome { }
SpecificSessionBean public interface HelloBean implements SessionBean { .. implemenation of methods provided in Hello Service }
ServiceLocator public class EJBHomeProvider { public static EJBLocalHome getEJBLocalHome(String jndiName) { /* Logic for creating a Home interface. Could have cached the context and home interface */ } }
StatelessServiceFactory public class StatelessServiceFactory { public static Service getService(String jndiName) { EJBLocalHome home = EJBHomeProvider.getEJBLocalHome(jndiName); Hello helloObj= home.create(); return helloObj; } }
Client public class SomeClient { .... HelloService helloService = (HelloService)StatelessServiceFactory.getService(ServiceConstants.HELLO); /* Here ServiceConstants could contain the list of all JNDI Names */ }
The above example can be extended to get any specific service. I think this pattern is more applicable with Local Stateless Session Beans and especially when there is excessive usage of many stateless session beans all over the application
Usually entity beans have overloaded create methods. And each type of entity bean could take different number of arugments in their create methods.
Hence it is not possible to completely avoid the invocation of create methods for entity beans.
(OR) even if we find a way to modify the getService method to take dynamic number or arguments in the form of an array or some other datastructure , it creates more confusion and is error prone than the problem it tries to solve.
|