In the "EJB 2.0 Development with WebSphere Studio Application Developer", i read : The EJB home component is comparable to a factory for the EJB objects. What does it mean clearly ? Thanks.
So, if i understand good, the purpose of using a factory class is to prevent from having many instances of a class ? The factory class will be responsible to return the same instance. Is right ? or i'm completely mistaken ?
Hi Engin This is incorrect. A factory is not used to prevent having too many instances of a class. There is a pattern that ensures that you have no more than one instance of a class--that pattern is called singleton.
A factory is used as an interface for object creation that allows subclasses to decide which object to instantiate. It is typically used when it cannot be anticipated what specific class needs to be created, or when you want to delegate responsibility of object creation to a helper.
The advantage of this is that the client code only has to deal with the interface, and not rely on some specific implementation. The design can then be more open.
A typical example of a factory is a when a container hands out datasource connections.
Eben Hewitt. SCJP, SCWCD, SCJD, SCJWSD for JEE 5, TOGAF 8 Certified Architect, author of Java SOA Cookbook (O'Reilly, 2009) and contributor to 97 Things Every Software Architect Should Know
Eben and Venicius thanks. 1. I saw that the factory class created an instance of the appropriated class. As you say, if we don't know wich database we will use, the factory will create an instance of the class wich handles the connection,drivers..with the concerned database. Until here is right ?
2.Then can we say also the factory is like a controller then ? I say it because in MVC (Swing) i have a controller wich instantiates the appropriated class for printing my screen. I give you here an example of my controller i have :
public class SecurityFopController extends FopController {
switch(report) { case ApplicationConstants.REP_SECURITY_CONSULTATION : mFilename = "Consultation"; // to define the file name of the PDF with the date mXSL = ApplicationConstants.REP_SECURITY_CONSULTATION_XSL; // to know wich XSL file to use mXMLBuilder = new ConsultationXMLBuilder((Consultation) comp); break;
case ApplicationConstants.REP_SECURITY_GESTION_GROUPE : mFilename = "Gestion_des_groupes"; // to define the file name of the PDF with the date mXSL = ApplicationConstants.REP_SECURITY_GESTION_GROUPE_XSL; // to know wich XSL file to use mXMLBuilder = new GestionGroupXMLBuilder((GestionGroupes) comp); break; } } }