Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is a factory ?

 
Engin Okucu
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the
 
Engin Okucu
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A factory is a common OO pattern. You can find some explanations at:
http://www.dofactory.com/Patterns/PatternAbstract.aspx

Hope it helps
 
Engin Okucu
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eusebio Floriano
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Engin Okucu
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any suggestion to my topic ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic