• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Instance factory methods

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Does anyone have an example or scenario where instance factory methods are applicable? What are their advantages over a regular constructors?
Thanks.
 
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out this JavaWorld article
 
Gina vernon
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Larry Frissell wrote:Check out this JavaWorld article


Larry,
Looks like the article is talking only about static factory methods. I am assuming static and instance factory methods are 2 seperate concepts. Right/wrong?
 
Larry Frissell
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am afraid that I do not know if those are two different concepts.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most factory methods are static because they have to dispense objects before an object of the class is created.

Apart from the error mentioned in the comments, the article Larry Frissell quoted looks quite good. You can find more, but I would recommend trying Joshua Bloch's book for more details.
 
Gina vernon
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Most factory methods are static because they have to dispense objects before an object of the class is created.

Apart from the error mentioned in the comments, the article Larry Frissell quoted looks quite good. You can find more, but I would recommend trying Joshua Bloch's book for more details.



Campbell,
I am looking for information about instance factory methods. Assuming they are not the same as static factory methods.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look in the javax.xml.transform package, you'll see TransformerFactory. It has a newInstance() method which produces a TransformerFactory. This is a classic static factory method; read its documentation and you will see why it exists (namely to aid in producing Transformers of classes only known at run time).

Now look at the newTransformer() method. This is a factory method, but this time it's an instance method. Why? Because the TransformerFactory that the newInstance() method produced belongs to some subclass of javax.xml.TransformerFactory, a subclass which knows how to produce a Transformer of the desired class.

Confused? I wouldn't be surprised. Let's suppose that we configured things to say that the Saxon transformation package should be used. Then we first use the standard javax.xml.TransformerFactory by calling its newInstance() method. This returns an instance of a class from the Saxon product which is a subclass of javax.xml.TransformerFactory. Next we use that instance by calling its newTransformer() method, and that returns a Saxon transformer.

So why is that better than a constructor? Well, this way we write our program to do XML transformation, and later we can decide to use Saxon instead of the built-in transformer, without having to change anything. In particular, we didn't have to write a constructor to create an instance of the Saxon transformer. We didn't even need to know that Saxon existed when we wrote the program, and yet we can still decide to use it.

 
Gina vernon
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,
Thanks for that explanation. I was able to understand how static and instance factory methods are better than constructors. I am also trying to understand the difference between static and instance factory methods so I know when to use them.
One difference is that static factory methods can return subtypes of their class while instance factory methods can't. Right/Wrong?
Second difference, static factory methods are capable of returning data types that are only known at runtime and instance factory methods are not. Right/Wrong?

Thanks.
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic