File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes Factory Method Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "Factory Method Pattern" Watch "Factory Method Pattern" New topic
Author

Factory Method Pattern

Pinal Patel
Greenhorn

Joined: Aug 02, 2003
Posts: 2
Hello Everyone,
I am really having a hard time understanding Factory Method Pattern (I tried GoF and it was little confuising), so if you could please explain this pattern in easy english with examples will help me understand better. I really appreciate your help on this in advance. Please reply.
Thanks & Regards,
Pinal
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
A factory method is a good way to create an object when you're not really sure at coding time exactly what class would be appropriate. Why would you not be sure? Maybe it's something based on your current configuration, like are we on Windows or Unix, on Oracle or UDB, is feature x turned on? Of course somewhere in the code you really have to decide what to create. The factory encapsulates that knowledge in one place so it can be used in many places.
If you see a switch or if-else-if-else structure around creating something, and then you see it again in another place, think about a factory. Worst case you write the case statement once inside the factory, which is still much better than writing it twice.
GoF is not real clear on this, and they confuse things even further by being fuzzy about Abstract Factory. Somebody else here at the ranch describe abstract factory well the other day. Suppose you want to create a whole family of things based on the same reasoning, maybe UI widgets. If I have one Factory, I might say Factory.getButton() Factory.getText() and so on. It would be bad if Factory had to repeat the same case logic. So we create a family of factories, each of which has NO case logic. We use the case logic to get the right factory: AbstractFactory.getConcreteFactory().getButton().
Hope that helped!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Billy Tsai
Ranch Hand

Joined: May 23, 2003
Posts: 1297
I just want to confirm what design patter is used for EJBHome,
is it abstract factory or factory method?
because i see some notes stating it is abstract factory and some other stating it is factory method and in whizlabs exam simulator it also stats EJBHome is factory method, so which design patter does EJBHome interface uses?


BEA 8.1 Certified Administrator, IBM Certified Solution Developer For XML 1.1 and Related Technologies, SCJP, SCWCD, SCBCD, SCDJWS, SCJD, SCEA,
Oracle Certified Master Java EE 5 Enterprise Architect
Goutam Bhattacharjee
Ranch Hand

Joined: Aug 05, 2003
Posts: 44
I am still confused trying to distinguish between Abstract Factory and Factory. Both of them seems similar to me. Can somebody explain in clear and simple terms?
thanx


gb
Nalla Senthilnathan
Ranch Hand

Joined: Jul 13, 2003
Posts: 40
Try these links
http://www.wikipedia.org/wiki/Abstract_factory_pattern
http://www.wikipedia.org/wiki/Factory_method_pattern
Nalla
Nick Thaker
Greenhorn

Joined: Jul 07, 2003
Posts: 7
I read this back in the late 90's and it's certainly an aid to memory if nothing else.
It's titled Non-Software Examples of Software Design Patterns.
http://wwwswt.informatik.uni-rostock.de/deutsch/Lehre/Uebung/Beispiele/PatternExamples/patexamples.htm
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Maybe it's just me, but those two pages said the same thing and didn't distinguish abstract factory at all.
Here's my take: could be wrong, might not match standardized tests, which I generally loathe anyhow.
Abstract factory tells you in the name there is an abstract class, which implies there must be one or more concrete factories. So your first step in using it is to get a concrete factory, then get the actual objects you want from the concrete factory. You might use this if you have a whole family of objects that have different versions based on the same criteria. Maybe I have 50 UI widgets with one set for Windows and another set for Unix. (ok, bad example for a platform independent language. sorry.) The abstract factory defines 50 getWidgetX() methods. One concrete factory would always give you Windows widgets, another concrete factory would always give you Unix widgets. To add a new platform, say Amiga OS, you'd add a new concrete factory. How do you get the right factory for your current platform? From a factory-factory!
Factory method is a similar thing on a much smaller scale. Maybe there is only one widget that varies from platform to platform, say a database connection object. Then the factory has a getThing() method that has logic inside it to determine the correct thing.
See the difference? If I had that family of 50 things with factory method I might have to put the logic in each of 50 methods, and update all 50 methods every time I add a new platform. Yuck!
BTW: I think these two patterns are poorly named and even more poorly described. And that could be because I'm completely wrong!
Alexander Mark
Greenhorn

Joined: Apr 06, 2002
Posts: 16
Try this link :
http://www.dofactory.com/Patterns/PatternAbstract.aspx
it's with C#, but sure... you can understand it with knowing Java,
as we know C# = Java Imitation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Factory Method Pattern
 
Similar Threads
confused with Design patterns
Client ID?
Design Patterns...
Factory design pattern
Abstract Factory pattern