aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes What is a factory ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "What is a factory ?" Watch "What is a factory ?" New topic
Author

What is a factory ?

Engin Okucu
Ranch Hand

Joined: Feb 09, 2002
Posts: 174
In the
Engin Okucu
Ranch Hand

Joined: Feb 09, 2002
Posts: 174
Sorry for the last message posted.

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.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
A factory is a common OO pattern. You can find some explanations at:
http://www.dofactory.com/Patterns/PatternAbstract.aspx

Hope it helps


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Engin Okucu
Ranch Hand

Joined: Feb 09, 2002
Posts: 174
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 ?
Eben Hewitt
Author
Ranch Hand

Joined: Apr 16, 2004
Posts: 36
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
Eusebio Floriano
Ranch Hand

Joined: Mar 07, 2004
Posts: 235
Exactly what Eben said.

You should use it when you want to hide how your object is created.
[ August 03, 2004: Message edited by: Vinicius Boson ]

SCJP 1.4 / 5.0 - SCBCD 1.3 - SCWCD 1.4 - IBM 484
Engin Okucu
Ranch Hand

Joined: Feb 09, 2002
Posts: 174
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 {

public SecurityFopController(int report, JComponent comp)
{
super(report, comp);
mModule = ApplicationConstants.MOD_SECURITY; // = 16

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;
}
}
}
Engin Okucu
Ranch Hand

Joined: Feb 09, 2002
Posts: 174
Any suggestion to my topic ?
 
jQuery in Action, 2nd edition
 
subject: What is a factory ?