File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes java factory objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java factory objects" Watch "java factory objects" New topic

java factory objects

doug shumway

Joined: May 29, 2002
Posts: 16
what exactly is meant by a java factory ?
Harpreet Hira
Ranch Hand

Joined: Sep 27, 2001
Posts: 72
There are some cases in which, a class has a private constructor. Such classes cannot be instantiated using new operator. Instead, a public static method is provided whose return type is the class itself( provides the instance of the its class). This static method is called the factory method and this class is called the factory class.
doug shumway

Joined: May 29, 2002
Posts: 16
thank you, I know the technique now I know the name
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
In which case these factory objects are useful? Any examples or reference reading for it?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

Two uses of the Factory pattern come to mind immediately (there are sure to be others with many more examples):
1) To control the number and way that objects are created. This could be for caching or pooling reasons, or simply to ensure that only certain instances of a class are created (Aside: I've seen Factory classes used when a Type-Safe Enum pattern would be more appropriate, but that's another show...).
2) When you want to create specific concrete instances of classes that extend an abstract class, or implement an interface, based upon run-time information.
As an example, let's say that we have a Widget interface that defines the attributes and behavior of widgets, but that there are vendor-specific differences on how the behaviors manifest themselves. All vendor widgets implement Widget so our code doesn't care which vendor's widget we have as long as it behaves correctly. The factory class is responsible for creating the correct concrete instance (perhaps using info passed to it, and/or environmental values) and returning the Widget-implementing object.
[ June 14, 2002: Message edited by: Bear Bibeault ]

[Asking smart questions] [About Bear] [Books by Bear]
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2591

You can read more abt it in Design Patterns, excellent book.
- Manish
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Also browse and linked pages

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
James W. Cooper's book, The Design Patterns Java Companion, is available as a free download at
The Factory Pattern is described in chapter one, beginning on page eighteen.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: java factory objects
It's not a secret anymore!