• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Abstract Factory Pattern question  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the abstract factory pattern suitable for factories that only create one product?
For example, if we have a VehicleFactory that only produces cars, do we need an AbstractVehicleFactory that returns a VehicleFactory that has a method createCar()?
I can see that the abstract factory pattern is usefull for factories that create different products such as a VehicleFactory class that creates cars and motorcylces.
An example of an abstract factory that only has a single product is the javax.xml.parsers.SAXParserFactory class.
SAXParserFactory.newInstance() returns a concrete SAXParserFactory instance.
SAXParserFactory has a non static method newSAXParser() that returns a SAXParser.
In this case, the only product it can create is a parser - nothing else.
Why do we even need to have an abstract factory in this case?
Could we not do something like this?
SAXParser parser = SAXParser.createParser();
Intead of this:
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
Again the question is:
Is the abstract factory pattern suitable for factories that only create one product?
Perhaps some wise sage could shed some light on this matter - thanks.
[ June 04, 2003: Message edited by: Lord British ]
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Lord British:
Could we not do something like this?
SAXParser parser = SAXParser.createParser();
Intead of this:
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();


How would you handle the setNamespaceAware and setValidating methods in the former case?
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing here ...
Why would we need an abstract factory to get a real factory? Because there could be different real factories. The abstract factory decides which one to give us. (How?)
Why would there be different real factories? Because we could be using different SAXParser implementations that provide their own real factories.
Does that sound right? I've never run into the need to make an abstract factory mice elf.
 
Lord British
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea, I can see that the abstract factory pattern being useful if you need to set certain attributes to the factory.
like:
vehicleFactory = VehicleFactory.newInstance();
vehicleFactory.setPaintColor(RED)
vehicleFactory.setEngineType(V8)
vehicleFactory.createCar();
[ June 05, 2003: Message edited by: Lord British ]
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am no wise sage, but I'll take a crack at it..

Originally posted by Lord British:
Is the abstract factory pattern suitable for factories that only create one product?


Per GoF's definition, Abstract Factory pattern is best used to provide an interface to create families of related or dependent products.


For example, if we have a VehicleFactory that only produces cars, do we need an AbstractVehicleFactory that returns a VehicleFactory that has a method createCar()?


If you have only one factory (~ VehicleFactory), no need of AbstractVehicleFactory. On the other hand, if you have factories in USA, Canada, India, Mexico producing different products/families of products,
you will have an AbstractVehicleFactory, which, based on runtime parameters (~ at which physical factory you are running the application which make use of the interface provided by AbstractVehicleFactory), will return an instance of one of these classes -
USAVehicleFactory, CanadaVehicleFactory, IndiaVehicleFactory, MexicoFactory.


I can see that the abstract factory pattern is usefull for factories that create different products such as a VehicleFactory class that creates cars and motorcylces.


Yes, indeed.


An example of an abstract factory that only has a single product is the javax.xml.parsers.SAXParserFactory class.


Not entirely true. Usually the default factory implementation is used, but, if you set the javax.xml.parsers.SAXParserFactory system property,
or, modify <JRE_Location>/lib/jaxp.properties, or .. see here at Java API documentation for more info, you can change the SAXParserFactory implementation (~USAVehicleFactory or CanadaVehicleFactory) used to create the instances of SAXParsers (~Car).


SAXParserFactory.newInstance() returns a concrete SAXParserFactory instance.


Actually the currently configured factory.


SAXParserFactory has a non static method newSAXParser() that returns a SAXParser.


It ia analogous to your newVehicle()


In this case, the only product it can create is a parser - nothing else.


This is not a limitation of the pattern.


Why do we even need to have an abstract factory in this case?
Could we not do something like this?
SAXParser parser = SAXParser.createParser();
Intead of this:
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();


What you have quoted here is a standard example of a pattern called "Factory method", which meets a requirement different from that of the AbstractFactory design pattern.


Again the question is:
Is the abstract factory pattern suitable for factories that only create one product?


Why not? This pattern is used to mainly switch between factories at runtime. The fact that the factory interface/implementation
creates, or allows the creation of only one product is a trivial detail, if not completely irrelevant.


Perhaps some wise sage could shed some light on this matter - thanks.
[ June 04, 2003: Message edited by: Lord British ]



-GB.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All hail, Lord British!
Please forgive my ignorance, I somehow failed to notice your regal presence. It's some time since I last saved your realm from the unholy "Guardian"...
Just for the unthinkable case that you are *not* *the* Lord British (again forgive my impertinence), but instead are just palming yourself off as somebody you aren't (shame on you!) - it is my duty to alert you of our Naming Policy and ask you to adjust your display name accordingly.
Thanks for your cooperation - and happy prowling on the Ranch!
Yours faithfully, Ilja
[ June 06, 2003: Message edited by: Ilja Preuss ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!