• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

java factory objects

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what exactly is meant by a java factory ?
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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( i.e.it 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
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you, I know the technique now I know the name
 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case these factory objects are useful? Any examples or reference reading for it?
regards,
Arun
 
Sheriff
Posts: 67269
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
hth,
bear
[ June 14, 2002: Message edited by: Bear Bibeault ]
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read more abt it in Design Patterns, excellent book.
HTH,
- Manish
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also browse http://c2.com/cgi/wiki?FactoryMethodPattern and linked pages
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James W. Cooper's book, The Design Patterns Java Companion, is available as a free download at http://www.patterndepot.com/put/8/JavaPatterns.htm
The Factory Pattern is described in chapter one, beginning on page eighteen.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!